I am new to C and am having a problem reading this matrix declaration in a function.
void foo(int (*array)[][3], int row, int col)
and in the function how do I access an array element - say to print it's value.
I am new to C and am having a problem reading this matrix declaration in a function.
void foo(int (*array)[][3], int row, int col)
and in the function how do I access an array element - say to print it's value.
int (*array)[][3]
declares array
to be a pointer to a 2D array whose second dimension is 3
. Example usage:
#include <stdio.h>
void foo(int (*array)[][3], int row, int col)
{
printf("%d\n", (*array)[row][col]);
}
int main()
{
int array[10][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
foo(&array, 2, 2);
return 0;
}
Output:
9
(*array)[][3]
is a pointer to 2D array.it can point to an int
array of variable rows and 3 columns.Here is an example:
int main(void)
{
int arr[3][3] =
{
{0,0,0},
{1,0,0},
{1,1,0},
};
int (*array)[3][3],row,col;
array = &arr;
printf("arr[1][0] : %d\n",(*array)[1][0]);
}