Why can we omit the first dimension of a multidimensional array when we are passing it to a function

后端 未结 2 852
-上瘾入骨i
-上瘾入骨i 2020-12-31 17:55

Why can we omit the first dimension of a multidimensional array when we are passing it to a function?

In my programming class, we were told what when passing a multi

2条回答
  •  -上瘾入骨i
    2020-12-31 18:33

    You cannot pass arrays to functions. Any function declared as taking an array type is automatically adjusted to take the corresponding pointer type. When you use the name of an array in a function call expression it decays to a pointer to its first element.

    These four declarations declare the same function:

    void f(int b[10][15][20]);
    void f(int (*b)[15][20]);
    void f(int b[][15][20]);
    void f(int b[100][15][20]);
    

    and, given int a[10][15][20];, these function calls are identical:

    f(a);
    f(&a[0]);
    

提交回复
热议问题