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

末鹿安然 提交于 2020-01-11 05:10:14

问题


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 calls this function:

- (void)testCall: (int[3][3]) arr {

    NSLog(@"cell value is %u",arr[1][1]);
}

I need the array to be of variable length - What is the best way to declare the function?

Using blanks doesn't work:

- (void)testCall: (int[][]) arr { 

Thanks for your help.


回答1:


I would write this as:

- (void) testCall: (int *) aMatrice;

Doing so allows you to avoid multiple mallocs and the math to calculate a single offset in a linear array based on x, y coordinates in a 2D array is trivial. It also avoids the multiple mallocs implied by int** and the limitations of 2D array syntax perpetuated by the language.

So, if you wanted a 4x5 array, you might do:

#define WIDTH 4
#define HEIGHT 5
#define INDEXOF(x,y) ((y*WIDTH) + x)

int *myArray = malloc(sizeof(int) * 5 * ELEMS_PER_ROW);

You could then initialize the array linearly or with a nested for loop:

for(int x=0; x<width; x++)
    for(int y=0; y<height; y++)
        myArray[INDEXOF(x,y)] = ... some value ...;

And you would pass it to the method like:

[foo testCall: myArray];

Though you might want to also carry along the width and the height or, better yet, create a IntMatrix subclass of NSObject that wraps all of the pointer arithmetic and storage beyond a nice clean API.

(all code typed into SO)




回答2:


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
}



回答3:


You can't use int[][] because the size of the second dimension affects how the array is laid out in memory. If you know the second dimension you can use int[][x], otherwise you'll have to use int** which can be accessed just like an array.




回答4:


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).




回答5:


call

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

function

- (void)testCall: (int *) arr 
{
    int (*V_arr)[3] = (int(*)[3])arr;
    NSLog(@"cell value is %u",V_arr[1][1]);
}


来源:https://stackoverflow.com/questions/1402250/passing-an-int-array-of-variable-length-as-a-function-parameter-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!