I know following function definitions are considered the same by c compiler
void test(int* array); // all converted to first one
void test(int array[]);
void test(int array[3]);
and in C99 standard
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.
my questions is after int array[] decays to a pointer, then it’s also a lvalue as local variable in function call, does it violate the standard that converted pointer should not be a lvalue?
>Solution :
There’s no problem as there’s two things in play here: the adjustment of array parameters to pointers, and how function parameters are set.
The equivalency of the three declarations you gave is spelled out in section 6.7.6.3p7 of the C standard:
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to
‘‘qualified pointer to type’’, where the type qualifiers (if any) are
those specified within the[and]of the array type derivation. If
the keyword static also appears within the[and]of the array type
derivation, then for each call to the function, the value of the
corresponding actual argument shall provide access to the first
element of an array with at least as many elements as specified by the
size expression
So this means that in all of the above cases, the type of the parameter array is int *.
Then there’s how parameters are populated. This is specified in 6.5.2.2p4 regarding function calls:
An argument may be an expression of any complete object type. In
preparing for the call to a function, the arguments are evaluated, and
each parameter is assigned the value of the corresponding argument
So given the following:
int a[5];
test(a);
Internally, there’s an assignment, i.e. array = a from the passed parameter value to the function’s actual parameter.