I am not asking how one would accomplish the end-goal of this code in a production environment. Rather, this question is about how C works. The given example is demonstrative, not practical.
Lets say I have 3 existing arrays, each of type int[10]
:
int first[10] = {0};
int second[10] = {0};
int third[10] = {0};
Lets say I want to create a 2d array which stores these arrays:
int outer[][10] = {
first,
second,
third,
};
The above would not work, as, in C, references directly to an array degenerate into pointers to the array's first element, meaning the above is stating:
// Not valid C, just demonstrative
int outer[][10] = {
int (*),
int (*),
int (*),
};
rather than the intended result of:
// Also not meant to be valid C
int outer[][10] = {
int[10],
int[10],
int[10]
};
Is it possible to initialize a 2d array in this way, with a list of array objects, rather than pointers to array elements?
int outer[][10] = { first, second, third, };
If this is not an option:
int* outer[][10] = {
first,
second,
third,
};
Then, consider the supported structure assignments:
typedef struct {
int _[10];
} array;
const array first = {0};
const array second = {0};
const array third = {0};
array outer[] = {
first,
second,
third,
};
The elements are accessed like outer[1]._[1] = 0;
.