How to pass a two-dimensional array of unknown size as an argument to a method

I am trying to pass a two-dimensional array, the size of which can be dynamic, as an argument to the method.

Inside the method, I would like to use an array with the syntax of a common array.

int item = array[row][column];

Array passing is not possible, so I was thinking about using a pointer pointer.

- (void)doSomethingWithArray:(int **)array columns:(int)nColumns rows:(int)nRows
{
   int item = array[n][m];
}

But I get a problem when I try to pass an array as a parameter

int array[numberOfRows][numberOfColumns];

[someObject doSomethingWithArray:array columns:numberOfColumns rows:numberOfRows];

I found a lot of tips and tricks, but for some reason nothing works the way I would like to use it.

Thanks for the help, Eny.

+5
source share
2 answers

Is objective-c based on C99?

, "" , .

#include <stdio.h>

void foo(int rows, int cols, int arr[rows][cols]) {
  printf("%d, %d\n", arr[0][0], arr[1][4]);
}

int main(void) {
  int arr[2][12] = {{1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}};
  foo(2, 12, arr);
}

, ideone.

+3
- (void)doSomethingWithArray:(void *)array columns:(int)nColumns rows:(int)nRows {}
...
[someObject doSomethingWithArray:&array columns:numberOfColumns rows:numberOfRows];
0

All Articles