What is the difference between the two functions in C?
void f1(double a[]) {
//...
}
void f2(double *a) {
//...
}
If I were to call the functions on a substantially long array, would these two functions behave differently, would they take more space on the stack?
First, some standardese:
So, in short, any function parameter declared as
T a[]
orT a[N]
is treated as though it were declaredT *a
.So, why are array parameters treated as though they were declared as pointers? Here's why:
Given the following code:
In the call to
foo
, the array expressionarr
isn't an operand of eithersizeof
or&
, so its type is implicitly converted from "10-element array ofint
" to "pointer toint
" 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
asbut it will be interpreted as
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
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:
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:
So there is no difference between the two versions of this call, at least the compiler threats them equally.