First of all this definition of a pointer
int array[10];
int **pointer = &array;
is invalid. In the right side of the declaration there is an expression having type int ( * )[10]
while in the left side there is identifier of type int **
. There is no implicit conversion between pointers int ( * )[10]
and int **
. So the compiler shall issue a diagnostic message.
The correct definition will look
int array[10];
int ( *pointer )[10] = &array;
Now we can consider what is the difference between these two definitions
int array[10];
int *pointer = array;
and
int array[10];
int ( *pointer )[10] = &array;
In the first case the size of the object pointed to by pointer pointer
is equal to sizeof( int )
. So if to use the pointer arithmetic then after evaluation of expression ++pointer
the value in the pointer will be increased by sizeof( int )
bytes. If for example sizeof( int )
is equal to 4
then the value in the pointer will be increased by 4
.
In the second case the size of the object pointed to by pointer pointer
is equal to 10 * sizeof( int )
that is if sizeof( int )
is equal to 4 then the size of the object is equal to 40
. So if the pointer will be increased ++pointer
its value will be increased by 40
.
Also dereferencing the pointer in the first case will give you an object of type int
while dereferencing the pointer in the second case will give you an object of type int[10]
that is an array.
And arrays are not pointers. Simply they are usually converted to pointers to their first elements in expressions. From the C Standard
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.
If you write for example
int array[10];
int *pointer = array;
then sizeof( array )
is not equal to sizeof( pointer )
though you can use the common syntax to access elements of the array:
array[ i ]
and pointer[ i ]
and will get the same result..