Passing a multidimensional array of variable size

前端 未结 2 1819
鱼传尺愫
鱼传尺愫 2020-12-03 07:58

I\'m trying to understand what \"best practice\" (or really any practice) is for passing a multidimensional array to a function in c is. Certainly this depends on the applic

2条回答
  •  渐次进展
    2020-12-03 08:39

    The easiest way is (for C99 and later)

    void printArry(int a, int b, int arr[a][b]){
        /* what goes here? */
    }
    

    But, there are other ways around

    void printArry(int a, int b, int arr[][b]){
        /* what goes here? */
    }
    

    or

    void printArry(int a, int b, int (*arr)[b]){
        /* what goes here? */
    }
    

    Compiler will adjust the first two to the third syntax. So, semantically all three are identical.

    And a little bit confusing which will work only as function prototype:

    void printArry(int a, int b, int arr[*][*]);
    

提交回复
热议问题