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

Discussion in 'C Programming' started by grocery_stocker, May 23, 2005.

  1. 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?
    grocery_stocker, May 23, 2005
    #1
    1. Advertising

  2. grocery_stocker

    Mick Sharpe Guest

    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 :)
    Mick Sharpe, May 23, 2005
    #2
    1. Advertising

  3. grocery_stocker

    -berlin.de Guest

    grocery_stocker <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, May 23, 2005
    #3
  4. grocery_stocker

    tigervamp Guest

    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
    tigervamp, May 24, 2005
    #4
  5. "Mick Sharpe" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 24, 2005
    #5
  6. "grocery_stocker" <> 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) <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.
    Keith Thompson, May 24, 2005
    #6
  7. On Mon, 23 May 2005 22:43:15 +0000 (UTC), "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);


    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>>
    Barry Schwarz, May 24, 2005
    #7
  8. Barry Schwarz <> writes:
    > On Mon, 23 May 2005 22:43:15 +0000 (UTC), "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);

    >
    > 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) <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.
    Keith Thompson, May 24, 2005
    #8
  9. grocery_stocker

    Mick Sharpe Guest

    You're right, of course, peeps - thanks for the corrections - it's been some
    years since I've written any C :-D
    Mick Sharpe, May 24, 2005
    #9
  10. Got it. Thanks for the clarification.
    grocery_stocker, May 25, 2005
    #10
  11. "grocery_stocker" <> 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) <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.
    Keith Thompson, May 25, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Hawtin
    Replies:
    2
    Views:
    374
    Eric Sosman
    May 4, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  3. Suresh V.

    Wouldn't it be nice if this worked?

    Suresh V., Aug 10, 2008, in forum: Python
    Replies:
    1
    Views:
    199
    Stefan Behnel
    Aug 10, 2008
  4. Casey Hawthorne
    Replies:
    5
    Views:
    393
    Arne Vajhøj
    Apr 25, 2009
  5. cyberco
    Replies:
    32
    Views:
    319
    Alan Garrison
    Dec 28, 2005
Loading...

Share This Page