473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why wouldn't this line of give give the address of the pointer?

Given:

int main(void) {

char *ptr = "test me";
printf("%s\n", &ptr[0]);
}
Why would the output be
test me

I thought & gave the address of the pointer. Why does this yield the
string?

Nov 14 '05 #1
10 1372
ptr[0] is the first char of the string and &ptr[0] is its address, which is
what %s is expecting. Try:

printf("%lx\n", (long)&ptr);

instead :-)
Nov 14 '05 #2
grocery_stocker <cd*****@gmail.com> wrote:
Given: int main(void) { char *ptr = "test me";
printf("%s\n", &ptr[0]);
}
You're missing a return statement here, you promised that main()
returns an int, didn't you?
Why would the output be
test me I thought & gave the address of the pointer. Why does this yield the
string?


Well, 'ptr' is a pointer to the (first element of the) string. But
you have it adorned with '[0]' after it and '&' before it. And the
'[]' operator "binds" stronger than the address operator, you would
have to use parentheses to change that. So 'ptr[0]' is evaluated
first, resulting in the first element of the array. Then the '&' in
front of that makes it a pointer again to this first element. So
'ptr' and '&ptr[0]' are identical (except that in the second form
you've got to type a bit more).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
Mick Sharpe wrote:
ptr[0] is the first char of the string and &ptr[0] is its address, which is what %s is expecting. Try:

printf("%lx\n", (long)&ptr);


Actually, this would probably be better:

printf("%p\n", (void *)&ptr);

if the representation of the pointer is not critical.

The conversion of a pointer to an integer type is implementation
defined behavior at best, undefined behavior at worst (if the pointer
cannot be represented as the specified integer type).

Rob Gamble

Nov 14 '05 #4
"Mick Sharpe" <mi*********@btinternet.com> writes:
ptr[0] is the first char of the string and &ptr[0] is its address, which is
what %s is expecting. Try:

printf("%lx\n", (long)&ptr);

instead :-)


No, don't do that. The "%lx" format expects an unsigned long
argument; you're passing a (signed) long. But that's probably not
going to cause any problems. The real problem is that you're
converting a pointer value to type long; this might or might not give
you a meaningful result.

The correct way to print a pointer is to use the "%p" format, which
expects a void*:

printf("%p\n", (void*)ptr);

The cast to void* is necessary because ptr is of type char*, and
printf with a "%p" format expects a void*. (Actually, it's not
strictly required in this case, because the language specifies that
void* and char* have the same representation, but that's a special
case and you're probably better off ignoring it.)

The above will print (some system-specific textual representation of)
the address of the memory location to which ptr points, which happens
to be the beginning of the string "test me".

Note that

printf("%p\n", (void*)&ptr);

is also legal, but does something different; it prints the address of
"ptr" itself.

--
keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
"grocery_stocker" <cd*****@gmail.com> writes:
Given:

int main(void) {

char *ptr = "test me";
printf("%s\n", &ptr[0]);
}
Why would the output be
test me

I thought & gave the address of the pointer. Why does this yield the
string?


The "%s" format tells printf() to expect a pointer to the first
element an array of char (to be printed as a string), and that's
exactly what you're giving it.

If you want to print the pointer value, use:

printf("%p\n", (void*)ptr);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
On Mon, 23 May 2005 22:43:15 +0000 (UTC), "Mick Sharpe"
<mi*********@btinternet.com> wrote:
ptr[0] is the first char of the string and &ptr[0] is its address, which is
what %s is expecting. Try:

printf("%lx\n", (long)&ptr);
The & contributes nothing to this effort. And %lx expects an unsigned
long.

instead :-)

Better still would be
printf("%p\n", (void*)ptr);
or the equivalent
printf("%p\n", (void*)&ptr[0]);

<<Remove the del for email>>
Nov 14 '05 #7
Barry Schwarz <sc******@deloz.net> writes:
On Mon, 23 May 2005 22:43:15 +0000 (UTC), "Mick Sharpe"
<mi*********@btinternet.com> wrote:
ptr[0] is the first char of the string and &ptr[0] is its address, which is
what %s is expecting. Try:

printf("%lx\n", (long)&ptr);


The & contributes nothing to this effort. And %lx expects an unsigned
long.


The "&" takes the address of the pointer object ptr. It's not
entirely clear whether this is what the OP is looking for. He
mentioned "the address of the pointer". He probably meant the address
to which the pointer points:

printf("%p\n", (void*)ptr);

but he may literally have meant the address of the pointer object:

printf("%p\n", (void*)&ptr);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
You're right, of course, peeps - thanks for the corrections - it's been some
years since I've written any C :-D
Nov 14 '05 #9
Got it. Thanks for the clarification.

Nov 14 '05 #10
"grocery_stocker" <cd*****@gmail.com> writes:
Got it. Thanks for the clarification.


Got what?

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
8
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
6
by: virgincita schmidtmann | last post by:
Good evening, I would like to pass the size of an array from the commandline. int main(int argc, int *argv) { .... max=*argv; int list; ....
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.