Pointer will not work in printf()

In this case, the compiler is just a bit overeager with the warnings. Your code is perfectly safe, you can optionally remove the warning with:

printf("Address of p1: %p\n", (void *) pt1);

Note that you get a simple warning. Your code will probably execute as expected.

The "%p" conversion specifier to printf expects a void* argument; pt1 is of type int*.

The warning is good because int* and void* may, on strange implementations, have different sizes or bit patterns or something.

Convert the int* to a void* with a cast ...

printf("%p\n", (void*)pt1);

... and all will be good, even on strange implementations.


Simply cast your int pointer to a void one:

printf( "Address of p1: %p\n", ( void * )pt1 );

Your code is safe, but you are compiling with the -Wformat warning flag, that will type check the calls to printf() and scanf().

Tags:

C

Printf

Pointers