-1

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.

  • 1
    It is an invalid syntax. Perhaps there is some context to it? – Eugene Sh. Nov 23 '15 at 19:37
  • This could be useful http://codinghighway.com/2013/12/29/the-absolute-definitive-guide-to-decipher-c-declarations/ – Minor Threat Nov 23 '15 at 19:37
  • This is probably an argument list of a function; in that case the function takes three arguments: a pointer to an array of arrays of 3 ints and two other ints. You can learn how to parse such declarations here: http://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations – user4520 Nov 23 '15 at 19:39
  • Possible duplicate of [How do I read this complex declaration in C](http://stackoverflow.com/questions/9500848/how-do-i-read-this-complex-declaration-in-c) – Jongware Nov 23 '15 at 20:27
  • @szczurcio Yes It is part of the function. I made the change in the question – Sakshi Malhotra Nov 23 '15 at 20:40
  • @cad It is part of a function declaration - made the edit – Sakshi Malhotra Nov 23 '15 at 20:42

2 Answers2

1
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
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

(*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]);
}
machine_1
  • 4,266
  • 2
  • 21
  • 42