Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why wouldn't this line of give give the address of the pointer?

Reply
Thread Tools

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

 
 
grocery_stocker
Guest
Posts: n/a
 
      05-23-2005
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?

 
Reply With Quote
 
 
 
 
Mick Sharpe
Guest
Posts: n/a
 
      05-23-2005
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


 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-23-2005
grocery_stocker <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
tigervamp
Guest
Posts: n/a
 
      05-24-2005
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-24-2005
"Mick Sharpe" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-24-2005
"grocery_stocker" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      05-24-2005
On Mon, 23 May 2005 22:43:15 +0000 (UTC), "Mick Sharpe"
<(E-Mail Removed)> 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>>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-24-2005
Barry Schwarz <(E-Mail Removed)> writes:
> On Mon, 23 May 2005 22:43:15 +0000 (UTC), "Mick Sharpe"
> <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Mick Sharpe
Guest
Posts: n/a
 
      05-24-2005
You're right, of course, peeps - thanks for the corrections - it's been some
years since I've written any C


 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      05-25-2005
Got it. Thanks for the clarification.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Why does address-of-char give me non-terminated c-string gibberish? sherifffruitfly@gmail.com C++ 10 10-25-2005 10:23 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments