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

G

grocery_stocker

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?
 
M

Mick Sharpe

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 :)
 
J

Jens.Toerring

grocery_stocker said:
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
 
T

tigervamp

Mick said:
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
 
K

Keith Thompson

Mick Sharpe said:
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.
 
K

Keith Thompson

grocery_stocker said:
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);
 
B

Barry Schwarz

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.
Better still would be
printf("%p\n", (void*)ptr);
or the equivalent
printf("%p\n", (void*)&ptr[0]);



<<Remove the del for email>>
 
K

Keith Thompson

Barry Schwarz said:
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);
 
M

Mick Sharpe

You're right, of course, peeps - thanks for the corrections - it's been some
years since I've written any C :-D
 
K

Keith Thompson

grocery_stocker said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top