I am currently studying C language. I wonder what 'array decaying' means, and when it happens.
And I wonder if the two variables below are interpreted in the same way.
char(*zippo)[2] = NULL;
char zippo2[4][2];
zippo = (char(*)[2])malloc(sizeof(char[2]) * 4);
From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
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.
The two variables below
char(*zippo)[2] = NULL;
char zippo2[4][2];
have different types. The first one is a pointer to an object of the type char[2]
. The second one is a two-dimensional array with four elements of the type char[2]
.
When the array zippo2
is used in an expression except the expressions listed in the quote (as for example using it with the sizeof
operator) then its designator is implicitly converted to pointer to its first element and has the same type as the variable zippo
.