How to return a two-dimensional pointer in C?

c, pointers

Solution

It helps to use a typedef for this:

typedef int MyArrayType[][5];

MyArrayType * foo(void)
{
    static int arr[5][5];
    return &arr;   // NB: return pointer to 2D array
}

If you don't want a use a typedef for some reason, or are just curious about what a naked version of the above function would look like, then the answer is this:

int (*foo(void))[][5]
{
    static int arr[5][5];
    return &arr;
}

Hopefully you can see why using a typedef is a good idea for such cases.

Problem

As the title suggests, how to return pointer like this: ``` xxxxxxx foo() { static int arr[5][5]; return arr; } ``` BTW. I know that I must specify the size of one dimension at least, but how?

Original source