Let us consider the following two programs in C language
Program #1
#include<stdio.h>
int main ()
{
int x = 5;
int *px;
px = &x;
printf ("&x = %p\n", &x);
printf ("px = %p\n", px);
return 0;
}
Program #2
#include<stdio.h>
int main()
{
int x = 5;
int *px = NULL;
px = &x;
printf("&x = %p\n", (void *)&x);
printf("px = %p\n", (void *)px);
return 0;
}
After compiling, I am getting no errors for both programs. But after compiling the #1, I am getting the following warning
format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’
After searching over stack exchange, I got that (void *) is a generic pointer, but my doubt is that, I declared px as a pointer variable to int, then why I need to cast to generic pointer instead of (int *). Why not %p can handle without type casting to (void *)?
Does it needed to cast to generic pointer whenever I print an address or pointer in-order to get zero warnings?