Making an array of 2D arrays in C

I hate I'm asking for help again. Unfortunately it seems there just aren't any links I can find on making an array that holds a bunch of two dimensional arrays. Maybe my google-fu is lacking. Basically I have a header file like this:

#define MATRIX 10

int white_l1[MATRIX][MATRIX];
int white_l2[MATRIX][MATRIX];
int white_l3[MATRIX][MATRIX];
int white_m1[MATRIX][MATRIX];
int white_m2[MATRIX][MATRIX];
int white_m3[MATRIX][MATRIX];
int white_r1[MATRIX][MATRIX];
int white_r2[MATRIX][MATRIX];
int white_r3[MATRIX][MATRIX];

int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3};

When I try to compile with it I get lots of the following:

mine.h:165:21: warning: initialization of �int *' from incompatible pointer type �int (*)[10]' [-Wincompatible-pointer-types]
 int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; /*
                     ^~~~~~~~
mine.h:165:21: note: (near initialization for �side_one[0]')
mine.h:165:31: warning: initialization of �int *' from incompatible pointer type �int (*)[10]' [-Wincompatible-pointer-types]
 int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; /*
                               ^~~~~~~~
mine.h:165:31: note: (near initialization for �side_one[1]')
mine.h:165:41: warning: initialization of �int *' from incompatible pointer type �int (*)[10]' [-Wincompatible-pointer-types]
 int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; /*

... etc, etc

I've tried making these lines pointers:

int *white_r3[MATRIX][MATRIX];

Then I just get similar output at compile time:

mine.h:165:91: note: (near initialization for �side_one[7]')
mine.h:165:101: warning: initialization of �int *' from incompatible pointer type �int * (*)[10]' [-Wincompatible-pointer-types]
 e_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; 

To be honest, I'm a little shaky on what �int * (*)[10]' even means. I would guess that a pointer to a pointer where one has been cast, but I'm probably wrong. At least I would think so from the code I used.

Any suggestions or insight much appreciated.

int * isn't enough information for C to know what kind of array is being pointed to. That assumes a single integer or 1D array.

int *matrix does not make pointers to 2d arrays -- it makes a 2d array of pointers.

Pointing to an array of known size takes this slightly wonky syntax:

int (*variablename) [elements];

The stuff inside the red brackets defines the array being pointed to, the [elements] outside it defines the actual number of elements.

An example:

int main(void)
{
        int arr2d[2][2]={ {1,2},{3,4} };
        int arr2db[2][2]={ {5,6},{7,8} };

        int (*ptr[2][2])[2]= { arr2d, arr2db };
}

I thank @yifangt for pointing out to me this was even possible. C has a few weird corners.

2 Likes

Thanks again Corona688. I used the following in the way you described and it compiled perfect!

int (*side_one[MATRIX][MATRIX])[MATRIX] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3};

I will marked this solved. Much appreciated.

1 Like