Difference between passing array and array pointer into function in C

前端 未结 3 1158
予麋鹿
予麋鹿 2020-11-22 04:00

What is the difference between the two functions in C?

void f1(double a[]) {
   //...
}

void f2(double *a) {
   //...
}

If I were to call

相关标签:
3条回答
  • 2020-11-22 04:23

    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 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, 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 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.

    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.

    0 讨论(0)
  • 2020-11-22 04:24

    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.

    0 讨论(0)
  • 2020-11-22 04:44

    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    ebp
    mov     ebp, esp
    sub     esp, 18h
    and     esp, 0FFFFFFF0h
    mov     eax, 0
    add     eax, 0Fh
    add     eax, 0Fh
    shr     eax, 4
    shl     eax, 4
    mov     [ebp+var_C], eax
    mov     eax, [ebp+var_C]
    call    sub_401730
    call    sub_4013D0
    mov     [ebp+var_8], 2
    mov     [ebp+var_4], 4
    lea     eax, [ebp+var_8]
    mov     [esp+18h+var_18], eax
    call    sub_401290
    call    _getch
    leave
    retn
    

    So there is no difference between the two versions of this call, at least the compiler threats them equally.

    0 讨论(0)
提交回复
热议问题