Passing an int array of variable length as a function parameter in Objective C

前端 未结 5 1697
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 13:04

I have the following code which works fine...

int testarr[3][3] = {
  {1,1,1},
  {1,0,1},
  {1,1,1}
};   
[self testCall: testarr];

Which c

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 13:58

    C arrays can't be variable in more than one dimension.

    You can't have this:

    int testarr[][] = {
      {1,1,1},
      {1,0,1,2},
      {1,1}
    };
    

    But you can have this:

    int testarr[][3] = {
      {1,1,1},
      {1,0,1},
      {1,1,1},
      {4,5,6},
      {7,8,9}
    }
    
    foo(testarr);
    
    void foo(int param[][3])
    {
        printf("%d", param[3][1]); // prints 5
    }
    

提交回复
热议问题