I am learning C and I am having some troubles to truly understand arrays and pointers in C, after a lot of research (reading the K&R book (still reading it), reading some articles and some answers here on SO) and taking tons of notes along the way, I've decided to make a little test / program to prove my understanding and to clear some doubts, but I'm yet confused by the warning I am getting using this code:
#include <stdio.h>
int main(int argc, char **argv) {
char a[3] = {1, 2, 3};
char *p = &a;
printf("%p\n", a); /* Memory address of the first element of the array */
printf("%p\n", &a); /* Memory address of the first element of the array */
printf("%d\n", *a); /* 1 */
}
Output compiling with GNU GCC v4.8.3:
warning: initialization from incompatible pointer type
char *p = &a;
^
0x7fffe526d090
0x7fffe526d090
1
Output compiling with VC++:
warning C4047: 'initializing' : 'char *' differs in levels of indirection from 'char (*)[3]'
00000000002EFBE0
00000000002EFBE0
1
Why am I getting this warning if &a
and a
are supposed to have the same value and compiling without warnings when using just a
as an initializer for p
?