Arrays are processed differently than other types; you cannot pass an array "by value" to C.
C99 Online Standard (Project n1256) , Section 6.3.2.1, βLvalues, Arrays, and Functions,β clause 3:
Unless it is an operand of the sizeof operator or a unary operator and the operator or string literal used to initialize the array, an expression that is of type '' array of type is converted to an expression with type '' pointer to type pointing to the starting element of the object array and is not an lvalue value. If the array object has a register storage class, the behavior is undefined.
In a call
foo(buf);
the buf array expression is not an operand of sizeof or & , and is not a string literal used to initialize the array, therefore it is implicitly converted ("decays") from the type "10- array of char elements" to "pointer to char", and the address of the first element passed to foo. Therefore, everything you do with buf in foo() will be reflected in the buf array in main() . Due to how the indexing of an array is determined, you can use the index operator by type of pointer to make it look like you are working with an array type, but it is not.
In the context of declaring a function parameter, T a[] and T a[N] are synonyms with T *a , but this is the only case where this is true.
John bode
source share