Passing a 2D array in a function in C program

后端 未结 3 1198
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 11:53

Below program (a toy program to pass around arrays to a function) doesn\'t compile. Please explain me, why is the compiler unable to compile(either because of techni

3条回答
  •  北海茫月
    2020-12-06 12:17

    This (as usual) is explained in the c faq. In a nutshell, an array decays to a pointer only once (after it decayed, the resulting pointer won't further decay).

    An array of arrays (i.e. a two-dimensional array in C) decays into a pointer to an array, not a pointer to a pointer.

    Easiest way to solve it:

    int **array; /* and then malloc */
    

提交回复
热议问题