1

This question is asked in some ways but am finding it difficult to understand.

int (*q)[3][4] -> q is a pointer to a 2-D array of 3 rows and 4 columns.

Now suppose I want to return a 2-d array from a function.

pointer-to-2d-array  func() {

     static 2 day array;

     return ;
}

Basically what should be the signature of my function. I have seen many places like below which I dont undesrtand

int (*fun2())[column];

Am finding it difficult to explain/understand the signature . Can anyone please help me on this?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
user3160866
  • 367
  • 2
  • 9
  • Please use [search](https://stackoverflow.com/search?q=) before asking questions. – 2501 Jul 17 '16 at 18:58
  • `void func(int (*p)[][COL_SIZE])` should do it. Note that row size is optional, which could automatically be evaluated. An array is passed to a function as a pointer, the changes in callee will reflect in caller too. So `void` as return type makes sense. – sjsam Jul 17 '16 at 19:07

1 Answers1

1

One simple approach is to use typedef:

typedef int (*ThreeByFourPtr)[3][4];

ThreeByFourPtr fun() {
    static int res[3][4]= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    return &res;
}

The advantage is that the syntax mirrors that of type pointer declaration, with type name replacing the name of the variable.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523