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

前端 未结 5 1640
佛祖请我去吃肉
佛祖请我去吃肉 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:59

    Why don't you just use NSArray or NSMutableArray with NSIntegers? Those array classes are of variable length, and much easier to use.

    This would result in

    - (void)testCall: (NSArray *) arr {
        NSLog(@"cell value is %u", [[arr objectAtIndex:1] objectAtIndex:1]);
    }
    

    (Of course, you would also have to define testarr using NSArray.)



    If you really want to use C arrays, making the method argument a pointer to an int with

    - (void)testCall: (int*) arr {

    will probably work (with the rest of the code staying the same).

提交回复
热议问题