C pointer notation compared to array notation: When passing to function

后端 未结 6 884
野的像风
野的像风 2020-12-01 18:52

My question is base on the following code:

int myfunct(int ary[], int arysize)   
int myfunct2(int *ary, int arysize)

 int main(void){
   int numary[10];
         


        
6条回答
  •  再見小時候
    2020-12-01 19:12

    When you declare a function parameter as an array, the compiler automatically ignores the array size (if any) and converts it to a pointer. That is, this declaration:

    int foo(char p[123]);
    

    is 100% equivalent to:

    int foo(char *p);
    

    In fact, this isn't about notation but about the actual type:

    typedef char array_t[42];
    int foo(array_t p);  // still the same function
    

    This has nothing to do with how you access p within the function. Furthermore, the [] operator is not "array notation". [] is a pointer operator:

    a[b]
    

    is 100% equivalent to:

    *(a + b)
    

提交回复
热议问题