if I write this, is it right?
No. This is not right. For %p
specifier you must have to cast it to void *
. Your compiler should give warning about this:
warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int **’ [-Werror=format=]
Read this answer. It says that:
The %p
format requires an argument of type void*
. If pointers of type int*
and int(*)[10]
have the same representation as void*
and are passed as arguments in the same way, as is the case for most implementations, it's likely to work, but it's not guaranteed. You should explicitly convert the pointers to void*
Draft n1570; 7.21.6 Formatted input/output functions:
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.
but theses two statements don’t return the same value if I put both of them in the program.
Yes,it will not return the same value. P
will give you the value of address of the variable p
points to (pointee) while &p
will give you the value of the address of pointer p
itself. Cast &p
to void *
.
printf("memory location of ptr: %p\n", (void *)&p);