The purpose of a pointer is to save the address of a specific variable. Then the memory structure of following code should look like:
int a = 5;
int *b = &a;
Why does this type of code generate a warning?
int a = 5; int *b = &a; int *c = &b;
The &
operator yields a pointer to the object, that is &a
is of type int *
so assigning (through initialization) it to b
which is also of type int *
is valid. &b
yields a pointer to object b
, that is &b
is of type pointer to int *
, i .e., int **
.
C says in the constraints of the assignment operator (which hold for the initialization) that (C11, 6.5.16.1p1): "both operands are pointers to qualified or unqualified versions of compatible types". But in the C definition of what is a compatible type int **
and int *
are not compatible types.
So there is a constraint violation in the int *c = &b;
initialization which means a diagnostic is required by the compiler.
One of the rationale of the rule here is there is no guarantee by the Standard that the two different pointer types are the same size (except for void *
and the character pointer types), that is sizeof (int *)
and sizeof (int **)
can be different values.