T
Tak
Hi :
I want to know how to use %p in the program.Help me!
Does it mean %x?
I want to know how to use %p in the program.Help me!
Does it mean %x?
Tak said:Hi :
I want to know how to use %p in the program.Help me!
Does it mean %x?
#include <stdio.h>cat format.c
dec -> 10gcc -Wall -o format format.c && ./format
Tak said:I want to know how to use %p in the program.Help me!
Does it mean %x?
printf("addr -> %p\n", &i);
Richard said:Pietro Cerutti <gahr_AT_gahr_DOT_ch_DO_NOT_SPAM> said:
Undefined behaviour. Use this instead:
printf("addr -> %p\n", (void *)&i);
I think the cast is unnecessary, unless int * and void *
are of different sizes in your system...
Tak said:Hi :
I want to know how to use %p in the program.Help me!
Does it mean %x?
Gilles said:even if they were, a void * isn't supposed to be able to store
a value held by another pointer of any kind ?
jacob said:I think the cast is unnecessary, unless int * and void *
are of different sizes in your system...
jacob said:>
> I think the cast is unnecessary, unless int * and void *
> are of different sizes in your system...
even if they were, a void * isn't supposed to be able to store
a value held by another pointer of any kind ?
The point is that it is possible, even if unlikely in most
architectures, for void * to have a different size and/or representation
to, for example, int *.
The %p format mask to printf() specifies that the argument is a void *,
not just "some pointer". If we are trying to be correct, rather than
simply getting away with it on some platform or other, we should pass a
void *.
Gilles Chehade said:
Any object pointer can be converted to and from void * without loss of
information. The cast is required; in its absence, the behaviour is
undefined. I do not dispute that Mr Navia thinks the cast to be
unnecessary unless int * and void * are different sizes but, regardless of
his thoughts on the matter, the cast remains necessary. The proof is in
three parts, the first of which is in 7.19.6.1, in the definition of
fprintf:
p 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.
The second part of the proof is in 7.19.6.3, the definition of printf:
2 The printf function is equivalent to fprintf with the argument stdout
interposed before the arguments to printf.
The third is in 4.2:
2 If a ``shall'' or ``shall not'' requirement that appears outside of a
constraint is violated, the behavior is undefined.
Mark said:The point is that it is possible, even if unlikely in most
architectures, for void * to have a different size and/or representation
to, for example, int *.
The %p format mask to printf() specifies that the argument is a void *,
not just "some pointer". If we are trying to be correct, rather than
simply getting away with it on some platform or other, we should pass a
void *.
Mark Bluemel said:Let's rephrase this. "If you know that int * and void * are represented
in the same way, both size and internal format, on your platform, AND
you never expect to use the code elsewhere,
you can skip the cast".
Pietro said:... a question raises:
1) two pointer types are allowed to be of different sizes
2) the void pointer is guaranteed to store any other pointer's value
without loss of information
this leads to:
3) the size of a void pointer is guaranteed to be at least as big as the
size of any other pointers
if 3) is a correct deduction, then I don't see the point to impose that
an argument of printf used in conjunction with %p must be a void pointer.
if 3) is incorrect, how do we assure that a conversion to void * cannot
lead to a loss of information?
Richard said:_And_ are passed the same way to variadic functions...
_And_...
I would not skip the cast for non-void pointers under any circumstances.
It is simply not worth the risk, and this is one of the few cases in C
where a mandatory cast actually _does_ make sense.
Mark Bluemel said:And formats...
[comp.lang.c] jacob navia said:Richard Heathfield wrote:printf("addr -> %p\n", (void *)&i);
I think the cast is unnecessary, unless int * and void *
are of different sizes in your system...
Richard said:I keep hearing this. Can you explain how,say, a "special" pointer can be
stored in a malloc'ed block of memory which basically returns a single
block from a common memory pool?
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.