Whats the use of %p

Discussion in 'C Programming' started by Spidey, Feb 24, 2006.

  1. Spidey

    Spidey Guest

    What kind of formating can be done with %p in printf
    Spidey, Feb 24, 2006
    #1
    1. Advertising

  2. Spidey

    Roka Guest

    Spidey wrote:
    > What kind of formating can be done with %p in printf


    To print out an address.
    eg.

    char *p = "abc";
    printf("The address of *abc* is %p\n",p);
    printf("The address of p is %p\n",&p);


    result may be:
    The address of *abc* is 0x804841c
    The address of p is 0xbffff474
    Roka, Feb 24, 2006
    #2
    1. Advertising

  3. Spidey

    Richard Bos Guest

    "Spidey" <> wrote:

    > What kind of formating can be done with %p in printf


    None. You can use it to print a pointer value; how the output is
    formatted is entirely up to the implementation.

    Richard
    Richard Bos, Feb 24, 2006
    #3
  4. "Roka" <> writes:
    > Spidey wrote:
    >> What kind of formating can be done with %p in printf

    >
    > To print out an address.
    > eg.
    >
    > char *p = "abc";
    > printf("The address of *abc* is %p\n",p);
    > printf("The address of p is %p\n",&p);
    >
    >
    > result may be:
    > The address of *abc* is 0x804841c
    > The address of p is 0xbffff474


    "%p" expects a void* argument. Giving it a char* is ok (but poor
    style IMHO); giving it char** is likely to work, but is strictly
    speaking non-portable.

    char *p = "abc";
    printf("The address of *abc* is %p\n", (void*)p);
    printf("The address of p is %p\n", (void*)&p);

    --
    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, Feb 24, 2006
    #4
  5. Spidey

    pete Guest

    Keith Thompson wrote:

    > "%p" expects a void* argument. Giving it a char* is ok (but poor
    > style IMHO);


    I don't like the wording of the standard in repeated footnotes
    on the issue of same representation
    meaning to imply interchangability.

    C99
    6.2.5 Types
    31)The same representation and alignment requirements are
    meant to imply interchangeability as arguments to
    functions, return values from functions, and members of
    unions.
    39)The same representation and alignment requirements are
    meant to imply interchangeability as arguments to
    functions, return values from functions, and members of
    unions.

    Why doesn't the normative part of the standard just say
    that it does or doesn't imply interchangeability?

    --
    pete
    pete, Feb 24, 2006
    #5
  6. Spidey

    Wojtek Lerch Guest

    "pete" <> wrote in message
    news:...
    > I don't like the wording of the standard in repeated footnotes
    > on the issue of same representation
    > meaning to imply interchangability.
    >
    > C99
    > 6.2.5 Types
    > 31)The same representation and alignment requirements are
    > meant to imply interchangeability as arguments to
    > functions, return values from functions, and members of
    > unions.
    > 39)The same representation and alignment requirements are
    > meant to imply interchangeability as arguments to
    > functions, return values from functions, and members of
    > unions.
    >
    > Why doesn't the normative part of the standard just say
    > that it does or doesn't imply interchangeability?


    Because it would then have to explain, in detail, what situations the
    interchangeability applies to, and that's a lot of work. To quote myself
    from another thread:

    : I know of two places in the normative text that describe situations where
    a
    : signed type and the corresponding unsigned type are interchangeable as
    : arguments to functions, and those two places are quite clear already:
    : 6.5.2.2p6 (calls to a function defined without a prototype) and 7.15.1.1p2
    : (va_arg()). If there are supposed to be more such situations, then I'm
    : afraid the footnote itself needs to be clarified. In particular, if the
    : only difference between two function types T1 and T2 is in the signedness
    of
    : parameters, was the intent that the two types are compatible, despite of
    : what 6.7.5.3p15 says? If not, which ones of the following were intended
    to
    : apply, if any:
    :
    : - it's OK to use an expression with type T1 to call a function that was
    : defined as T2, even though 6.5.2.2p6 says it's undefined behaviour?
    :
    : - it's OK to declare the function as T1 in one translation unit and define
    : as T2 in another translation unit, even though 6.2.7p1 says it's undefined
    : behaviour?
    :
    : - it's OK to define the function as T1 and then as T2 in *the same*
    : translation unit, even though 6.7p4 says it's a constraint violation?
    :
    : What about interchangeability as return values from function? I haven't
    : found any normative text that implies this kind of interchangeability;
    which
    : of the above three situations are meant to apply if T1 and T2 have
    different
    : return types?
    Wojtek Lerch, Feb 24, 2006
    #6
  7. Spidey

    pete Guest

    Wojtek Lerch wrote:
    >
    > "pete" <> wrote in message
    > news:...
    > > I don't like the wording of the standard in repeated footnotes
    > > on the issue of same representation
    > > meaning to imply interchangability.
    > >
    > > C99
    > > 6.2.5 Types
    > > 31)The same representation and alignment requirements are
    > > meant to imply interchangeability as arguments to
    > > functions, return values from functions, and members of
    > > unions.
    > > 39)The same representation and alignment requirements are
    > > meant to imply interchangeability as arguments to
    > > functions, return values from functions, and members of
    > > unions.
    > >
    > > Why doesn't the normative part of the standard just say
    > > that it does or doesn't imply interchangeability?

    >
    > Because it would then have to explain, in detail, what situations the
    > interchangeability applies to, and that's a lot of work. To quote myself
    > from another thread:
    >
    > : I know of two places in the normative text that describe situations where
    > a
    > : signed type and the corresponding unsigned type are interchangeable as
    > : arguments to functions, and those two places are quite clear already:
    > : 6.5.2.2p6 (calls to a function defined without a prototype) and 7.15.1.1p2
    > : (va_arg()). If there are supposed to be more such situations, then I'm
    > : afraid the footnote itself needs to be clarified. In particular, if the
    > : only difference between two function types T1 and T2 is in the signedness
    > of
    > : parameters, was the intent that the two types are compatible, despite of
    > : what 6.7.5.3p15 says? If not, which ones of the following were intended
    > to
    > : apply, if any:
    > :
    > : - it's OK to use an expression with type T1 to call a function that was
    > : defined as T2, even though 6.5.2.2p6 says it's undefined behaviour?
    > :
    > : - it's OK to declare the function as T1 in one translation unit and define
    > : as T2 in another translation unit, even though 6.2.7p1 says it's undefined
    > : behaviour?
    > :
    > : - it's OK to define the function as T1 and then as T2 in *the same*
    > : translation unit, even though 6.7p4 says it's a constraint violation?
    > :
    > : What about interchangeability as return values from function? I haven't
    > : found any normative text that implies this kind of interchangeability;
    > which
    > : of the above three situations are meant to apply if T1 and T2 have
    > different
    > : return types?


    I like to use linked lists with generic data pointers.
    Is the cast in new.c, completely redundant?

    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    struct list_node {
    struct list_node *next;
    void *data;
    } node;
    char *string = "string";

    node.data = string;
    puts((char *)node.data);
    return 0;
    }

    /* END new.c */

    --
    pete
    pete, Feb 24, 2006
    #7
  8. Spidey

    Wojtek Lerch Guest

    "pete" <> wrote in message
    news:...
    > Is the cast in new.c, completely redundant?

    ....
    > #include <stdio.h>

    ....
    > struct list_node {

    ....
    > void *data;
    > } node;

    ....
    > puts((char *)node.data);


    Yes, because <stdio.h> provides a prototype for puts() that causes an
    implicit conversion. But if you used printf and %s instead, it would depend
    on whether you want to rely on what "everybody knows" the intent was, or
    only on what a strict reading of the words promises (and depending on who
    reads them).
    Wojtek Lerch, Feb 24, 2006
    #8
  9. Spidey

    pete Guest

    Wojtek Lerch wrote:
    >
    > "pete" <> wrote in message
    > news:...
    > > Is the cast in new.c, completely redundant?

    > ...
    > > #include <stdio.h>

    > ...
    > > struct list_node {

    > ...
    > > void *data;
    > > } node;

    > ...
    > > puts((char *)node.data);

    >
    > Yes, because <stdio.h> provides a prototype for puts() that causes an
    > implicit conversion.
    > But if you used printf and %s instead,


    part_fprint is the example that I should have given,
    because I actually do use the fprintf function that way.

    void part_fprint(FILE *stream, list_type *node)
    {
    while (node != NULL) {
    fprintf(stream, "%s\n", (char *)node -> data);
    node = node -> next;
    }
    }

    > it would depend
    > on whether you want to rely on what "everybody knows"
    > the intent was, or only on what a strict reading of the
    > words promises (and depending on who reads them).


    Thank you.
    I'll keep the cast in part_fprint.

    --
    pete
    pete, Feb 24, 2006
    #9
  10. Spidey

    Wojtek Lerch Guest

    Wojtek Lerch wrote:
    > : - it's OK to define the function as T1 and then as T2 in *the same*

    ^^^^^^
    I meant "declare", of course. :)
    > : translation unit, even though 6.7p4 says it's a constraint violation?
    Wojtek Lerch, Feb 24, 2006
    #10
  11. pete wrote:
    > Why doesn't the normative part of the standard just say
    > that it does or doesn't imply interchangeability?


    Because that would require a whole lot of additional
    definition of terms (some of them hard to define
    exactly the way we want them), which we can leave up
    to "common understanding" when we use them in footnotes.
    Douglas A. Gwyn, Feb 25, 2006
    #11
  12. Spidey

    Malcolm Guest

    >
    > What kind of formating can be done with %p in printf
    >

    There's never any point in printing out a pointer except to debug code.
    Hence no reason to format nicely for the user's consumption.
    --
    Buy my book 12 Common Atheist Arguments (refuted)
    $1.25 download or $6.90 paper, available www.lulu.com
    Malcolm, Feb 25, 2006
    #12
  13. Spidey

    SM Ryan Guest

    "Malcolm" <> wrote:
    # >
    # > What kind of formating can be done with %p in printf
    # >
    # There's never any point in printing out a pointer except to debug code.
    # Hence no reason to format nicely for the user's consumption.

    Creates unique strings for things like hash table keys.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I love the smell of commerce in the morning.
    SM Ryan, Feb 26, 2006
    #13
  14. Spidey

    MrG{DRGN} Guest

    "Richard Bos" <> wrote in message
    news:4all.nl...
    > "Spidey" <> wrote:
    >
    >> What kind of formating can be done with %p in printf

    >
    > None. You can use it to print a pointer value; how the output is
    > formatted is entirely up to the implementation.


    Richard by the above do you mean that the output of %p is not guaranteed to
    be in hexadecimal format?(as shown elsethread, and also the output on my
    with my compiler/computer)

    Thanks

    --
    MrG{DRGN}
    MrG{DRGN}, Feb 26, 2006
    #14
  15. "MrG{DRGN}" <> writes:
    > "Richard Bos" <> wrote in message
    > news:4all.nl...
    >> "Spidey" <> wrote:
    >>
    >>> What kind of formating can be done with %p in printf

    >>
    >> None. You can use it to print a pointer value; how the output is
    >> formatted is entirely up to the implementation.

    >
    > Richard by the above do you mean that the output of %p is not guaranteed to
    > be in hexadecimal format?(as shown elsethread, and also the output on my
    > with my compiler/computer)


    Correct; it's not even guaranteed to look anything like a number.

    Quoting the standard:

    The argument shall be a pointer to void. The value of the pointer
    is converted to a sequence of printing characters, in an
    implementation-defined manner.

    --
    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, Feb 26, 2006
    #15
  16. Spidey

    CBFalconer Guest

    Keith Thompson wrote:
    > "MrG{DRGN}" <> writes:
    >> "Richard Bos" <> wrote in message
    >>> "Spidey" <> wrote:
    >>>
    >>>> What kind of formating can be done with %p in printf
    >>>
    >>> None. You can use it to print a pointer value; how the output is
    >>> formatted is entirely up to the implementation.

    >>
    >> Richard by the above do you mean that the output of %p is not
    >> guaranteed to be in hexadecimal format?(as shown elsethread, and
    >> also the output on my with my compiler/computer)

    >
    > Correct; it's not even guaranteed to look anything like a number.
    >
    > Quoting the standard:
    >
    > The argument shall be a pointer to void. The value of the
    > pointer is converted to a sequence of printing characters,
    > in an implementation-defined manner.


    Is it even guaranteed to vary across different pointers? I imagine
    an output of, say, "here lieth a soggy pointer" would meet the
    standard. It might not be considered an especially high quality
    implementation.

    --
    "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
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, Feb 26, 2006
    #16
  17. Spidey

    MrG{DRGN} Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "MrG{DRGN}" <> writes:
    >> "Richard Bos" <> wrote in message
    >> news:4all.nl...
    >>> "Spidey" <> wrote:
    >>>
    >>>> What kind of formating can be done with %p in printf
    >>>
    >>> None. You can use it to print a pointer value; how the output is
    >>> formatted is entirely up to the implementation.

    >>
    >> Richard by the above do you mean that the output of %p is not guaranteed
    >> to
    >> be in hexadecimal format?(as shown elsethread, and also the output on my
    >> with my compiler/computer)

    >
    > Correct; it's not even guaranteed to look anything like a number.
    >
    > Quoting the standard:
    >
    > The argument shall be a pointer to void. The value of the pointer
    > is converted to a sequence of printing characters, in an
    > implementation-defined manner.
    >



    Thanks for the info Keith! Ok then my next questions at this point would be
    these. What (if any) is/are the use(s) of %p in printf in a situation where
    the output isn't formatted as a number of some sort? Are you still able to
    determine the address by somehow deciphering the non-numerical sequence of
    printing characters? If not then is there any portable usefulness for %p in
    printf? Sorry for all the questions. I'm really pretty new to C, and am self
    taught mainly from working with the Quake 2 source code, and some (I found
    out after I bought them) obsolete and/or poorly written C programming books.
    (Schildt's Teach yourself C, and Oualline's Practical C programming) I don't
    have much free cash right now to pickup any better books, but any
    suggestions would be welcome, as I should have a bit of non-allotted cash
    when my income tax refunds come in. In addition, I want to say I'm sorry if
    I've hijacked this thread a bit too much from the original poster!

    Thanks


    --
    MrG{DRGN}
    MrG{DRGN}, Feb 26, 2006
    #17
  18. "MrG{DRGN}" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> "MrG{DRGN}" <> writes:
    >>> "Richard Bos" <> wrote in message
    >>> news:4all.nl...

    [...]
    >>>> None. You can use it to print a pointer value; how the output is
    >>>> formatted is entirely up to the implementation.
    >>>
    >>> Richard by the above do you mean that the output of %p is not
    >>> guaranteed to be in hexadecimal format?(as shown elsethread, and
    >>> also the output on my with my compiler/computer)

    >>
    >> Correct; it's not even guaranteed to look anything like a number.
    >>
    >> Quoting the standard:
    >>
    >> The argument shall be a pointer to void. The value of the pointer
    >> is converted to a sequence of printing characters, in an
    >> implementation-defined manner.

    >
    > Thanks for the info Keith! Ok then my next questions at this point would be
    > these. What (if any) is/are the use(s) of %p in printf in a situation where
    > the output isn't formatted as a number of some sort? Are you still able to
    > determine the address by somehow deciphering the non-numerical sequence of
    > printing characters? If not then is there any portable usefulness for %p in
    > printf?

    [...]

    Presumably an implementation will display a pointer in whatever format
    makes the most sense. On a system with segmented addressing, for
    example, "%p" might display something like "dead:beef".

    The most important thing to know about pointers is this: Pointers are
    not integers. (Read section 4 of the FAQ if you haven't already.)

    > In addition, I want to say I'm sorry if
    > I've hijacked this thread a bit too much from the original poster!


    No problem; we're still talking about C, which is in improvement over
    a lot of threads I've seen here.

    --
    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, Feb 26, 2006
    #18
  19. Spidey

    Michael Mair Guest

    MrG{DRGN} schrieb:
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >>"MrG{DRGN}" <> writes:
    >>
    >>>"Richard Bos" <> wrote in message
    >>>news:4all.nl...
    >>>
    >>>>"Spidey" <> wrote:
    >>>>
    >>>>
    >>>>>What kind of formating can be done with %p in printf
    >>>>
    >>>>None. You can use it to print a pointer value; how the output is
    >>>>formatted is entirely up to the implementation.
    >>>
    >>>Richard by the above do you mean that the output of %p is not guaranteed
    >>>to
    >>>be in hexadecimal format?(as shown elsethread, and also the output on my
    >>>with my compiler/computer)

    >>
    >>Correct; it's not even guaranteed to look anything like a number.
    >>
    >>Quoting the standard:
    >>
    >> The argument shall be a pointer to void. The value of the pointer
    >> is converted to a sequence of printing characters, in an
    >> implementation-defined manner.

    >
    > Thanks for the info Keith! Ok then my next questions at this point would be
    > these. What (if any) is/are the use(s) of %p in printf in a situation where
    > the output isn't formatted as a number of some sort? Are you still able to
    > determine the address by somehow deciphering the non-numerical sequence of
    > printing characters? If not then is there any portable usefulness for %p in
    > printf?


    You can still use the output as debugging help or to get
    unique strings. Or read this address in with sscanf()
    (within the usual restrictions).
    If you use the address for anything else, you are no longer
    in the realm of portable C...
    In addition, "physical: 0x1234AFFE virtual: 0xAFFE" or
    "data: 0xAFFE"(objects)/"code: 0x1234"(function pointers)
    are not strictly numbers but may be more useful...

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Feb 26, 2006
    #19
  20. Spidey

    Jordan Abel Guest

    On 2006-02-26, CBFalconer <> wrote:
    > Keith Thompson wrote:
    >> "MrG{DRGN}" <> writes:
    >>> "Richard Bos" <> wrote in message
    >>>> "Spidey" <> wrote:
    >>>>
    >>>>> What kind of formating can be done with %p in printf
    >>>>
    >>>> None. You can use it to print a pointer value; how the output is
    >>>> formatted is entirely up to the implementation.
    >>>
    >>> Richard by the above do you mean that the output of %p is not
    >>> guaranteed to be in hexadecimal format?(as shown elsethread, and
    >>> also the output on my with my compiler/computer)

    >>
    >> Correct; it's not even guaranteed to look anything like a number.
    >>
    >> Quoting the standard:
    >>
    >> The argument shall be a pointer to void. The value of the
    >> pointer is converted to a sequence of printing characters,
    >> in an implementation-defined manner.

    >
    > Is it even guaranteed to vary across different pointers? I imagine
    > an output of, say, "here lieth a soggy pointer" would meet the
    > standard. It might not be considered an especially high quality
    > implementation.


    I believe it's guaranteed that you can feed it to scanf %p and get a
    pointer that is the same as [compares equal to] the original.
    Jordan Abel, Feb 26, 2006
    #20
    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. Madni
    Replies:
    4
    Views:
    325
    Adam Maass
    Jun 12, 2006
  2. Replies:
    6
    Views:
    677
    James McIninch
    Jan 11, 2005
  3. sam
    Replies:
    4
    Views:
    364
    Victor Bazarov
    Jun 26, 2007
  4. Tricky
    Replies:
    1
    Views:
    419
  5. arnuld

    whats the use of unsigned char

    arnuld, Nov 6, 2009, in forum: C Programming
    Replies:
    11
    Views:
    1,540
    Seebs
    Nov 6, 2009
Loading...

Share This Page