S
Spidey
What kind of formating can be done with %p in printf
Spidey said:What kind of formating can be done with %p in printf
Spidey said:What kind of formating can be done with %p in printf
Roka said: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);
pete said: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?
Wojtek said: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?
pete said:Is the cast in new.c, completely redundant? ....
#include <stdio.h> ....
struct list_node { ....
void *data;
} node; ....
puts((char *)node.data);
Wojtek said: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 said:: - it's OK to define the function as T1 and then as T2 in *the same*
pete said:Why doesn't the normative part of the standard just say
that it does or doesn't imply interchangeability?
There's never any point in printing out a pointer except to debug code.What kind of formating can be done with %p in printf
Richard Bos said:None. You can use it to print a pointer value; how the output is
formatted is entirely up to the implementation.
MrG{DRGN} said: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)
Keith said: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 said: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.
[...]MrG{DRGN} said:Keith Thompson said:MrG{DRGN} said:news:[email protected]... [...]
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?
In addition, I want to say I'm sorry if
I've hijacked this thread a bit too much from the original poster!
MrG{DRGN} said: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?
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.
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.