Difference between passing array and array pointer into function in C

前端 未结 3 1164
予麋鹿
予麋鹿 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:44

    No, there is no difference between them. To test I wrote this C code in Dev C++(mingw) compiler:

    #include 
    
    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.

提交回复
热议问题