You will need a pointer to int for that purpose. C does not have a bultin 2D array, but you have ways to work around that. You have two options here:
Simulate a 2D array thru helper function
either you simulate the 2nd dimension; I suggest an helper function for that purpose, something like:
int getTwoDimensionArray(int ** array, int rowSize, int x, int y)
{
return (*array)[x + y*rowSize];
}
void setTwoDimensionArray(int ** array, int rowSize, int x, int y, int value)
{
(*array)[x + y*rowSize] = value;
}
Let say you want to allocate a 5x7 array dynamically, you would use those functions like such:
First allocate the 1D array:
int * myArray = (int*) malloc(5*7);
After, you would set value of 10 at position x=2, y=2 like that
setTwoDimensionArray(&myArray, 5, 2, 2, 10);
And later, if you want to get back the value:
int value = getTwoDimensionArray(&myArray, 5, 2, 2);
Have an array of arrays so simulate a 2D array
int getTwoDimensionArray(int ** array, int rowSize, int x, int y)
{
return (*array)[x][y];
}
void setTwoDimensionArray(int ** array, int rowSize, int x, int y, int value)
{
(*array)[x][y] = value;
}
Allocation of the array of array:
int ** myArray = (int**) malloc(5);
for ( int i=0; i<5; ++i )
{
myArray[i] = (int*) malloc(7);
}
You would use the helper functions the same way you did with option #1
setTwoDimensionArray(&myArray, 5, 2, 2, 10);
int value = getTwoDimensionArray(&myArray, 5, 2, 2);
C++
Of course, if you can use C++, you have more options, see Multidimensional Containers in C++