1

The following code snippet proofs that both are the same:

int a[4];
printf("a: %p\n&a: %p", a, &a);
"0x12345678"
"0x12345678"

But the compiler will warn in case of:

int a[4], *p;
p = &a; 

assignment from incompatible pointer type

Which pointer type does &a have ?

mr.wolle
  • 1,148
  • 2
  • 13
  • 21

1 Answers1

3

To be correct, first, your print statement should look like

 printf("a: %p\n&a: %p", (void *)a, (void *)&a);

because %p expects a void * argument. Please, note, printf() being a variadic function, implicit conversion (cast) won't take place, so, the casting is required.

Now, that said, a being an array,

  • a is of type int [4]
  • &a is if type int (*)[4]

(but, both will return the same address, FWIW.) You can also see this answer.

OTOH, in your case, p is of type int *.

That's why your compiler warns you about the type mismatch in the assignment.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261