On testing the code
#include <stdio.h>
int main()
{
char a[5][3];
printf("a = %p\n", a);
printf("&a[0] = %p\n", &a[0][0]);
printf("&a = %p\n", &a);
printf("*a = %p\n", *a);
return 0;
}
it get compiled and giving the output with C mode (http://ideone.com/KD9Wz1):
a = 0xbfd8ea51
&a[0] = 0xbfd8ea51
&a = 0xbfd8ea51
*a = 0xbfd8ea51
While compiling it with strict C99 mode (http://ideone.com/iTACGZ), it results in compilation error:
prog.c: In function ‘main’:
prog.c:7:10: error: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘char (*)[3]’ [-Werror=format]
prog.c:9:10: error: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘char (*)[5][3]’ [-Werror=format]
cc1: all warnings being treated as errors
Isn't the above code is valid in C99?
You need to insert an explicit cast to void* for any pointer except char*, because the standard guarantees that character pointers and void pointers have identical representation:
6.2.5.27: A pointer to
voidshall have the same representation and alignment requirements as a pointer to a character type.
Your C89 program has the same problem, but the compiler does not catch it.
Here is the fix:
printf("a = %p\n", (void*)a);
printf("&a[0] = %p\n", &a[0][0]);
printf("&a = %p\n", (void*)&a);
printf("*a = %p\n", *a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With