Initialization from incompatible pointer type warning when assigning to a pointer

  • &arr gives an array pointer, a special pointer type int(*)[5] which points at the array as whole.
  • arr, when written in an expression such a int *q = arr;, “decays” into a pointer to the first element. Completely equivalent to int *q = &arr[0];

In the first case you try to assign a int(*)[5] to a int*. These are incompatible pointer types, hence the compiler diagnostic message.

As it turns out, the array pointer and the int pointer to the first element will very likely have the same representation and the same address internally. This is why the first example “works” even though it is not correct C.

Leave a Comment