Difference between passing array and array pointer into function in C
First, some standardese:
6.7.5.3 Function declarators (including prototypes)
...
7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer totype’’, where the type qualifiers (if any) are those specified within the[
and]
of thearray type derivation. If the keywordstatic
also appears within the[
and]
of thearray type derivation, then for each call to the function, the value of the correspondingactual argument shall provide access to the first element of an array with at least as manyelements as specified by the size expression.
So, in short, any function parameter declared as T a[]
or T a[N]
is treated as though it were declared T *a
.
So, why are array parameters treated as though they were declared as pointers? Here's why:
6.3.2.1 Lvalues, arrays, and function designators
...
3 Except when it is the operand of thesizeof
operator or the unary&
operator, or is astring literal used to initialize an array, an expression that has type ‘‘array of type’’ isconverted to an expression with type ‘‘pointer to type’’ that points to the initial element ofthe array object and is not an lvalue. If the array object has register storage class, thebehavior is undefined.
Given the following code:
int main(void){ int arr[10]; foo(arr); ...}
In the call to foo
, the array expression arr
isn't an operand of either sizeof
or &
, so its type is implicitly converted from "10-element array of int
" to "pointer to int
" according to 6.2.3.1/3. Thus, foo
will receive a pointer value, rather than an array value.
Because of 6.7.5.3/7, you can write foo
as
void foo(int a[]) // or int a[10]{ ...}
but it will be interpreted as
void foo(int *a){ ...}
Thus, the two forms are identical.
The last sentence in 6.7.5.3/7 was introduced with C99, and basically means that if you have a parameter declaration like
void foo(int a[static 10]){ ...}
the actual parameter corresponding to a
must be an array with at least 10 elements.
The difference is purely syntaxic. In C, when the array notation is used for a function parameter, it is automatically transformed into a pointer declaration.
No, there is no difference between them. To test I wrote this C code in Dev C++(mingw) compiler:
#include <stdio.h>void function(int* array) { int a =5;}void main() { int array[]={2,4}; function(array); getch();}
When I disassemble main function in .exe of both calling versions of binary file in IDA I get exactly the same assembly code like below:
push ebpmov ebp, espsub esp, 18hand esp, 0FFFFFFF0hmov eax, 0add eax, 0Fhadd eax, 0Fhshr eax, 4shl eax, 4mov [ebp+var_C], eaxmov eax, [ebp+var_C]call sub_401730call sub_4013D0mov [ebp+var_8], 2mov [ebp+var_4], 4lea eax, [ebp+var_8]mov [esp+18h+var_18], eaxcall sub_401290call _getchleaveretn
So there is no difference between the two versions of this call, at least the compiler threats them equally.