How do i create a three dimensional array where only one of the dimensions is known at compile time.
The content of the array is struct values as
struct mat
{
char x[3];
int a;
}
struct samp
{
int a;
struct mat;
}
The array is supposed to store ‘samp’ and its size is as
struct samp samp_arr[unknown][10][unknown];
the first time the program runs the first dimension of samp_arr will be one and the last dimension will grow with the number of samp structures put into the array. After a while the first dimension should be incremented by one and any undefined number of samp structs will be put into it. And so on
If you have a C99 complying compiler you don’t have to re-invent the wheel, multi-dimensional arrays even with dynamic bounds are part of the language.
(supposing that
unknownis an expression that evaluates to the value of your liking.)Usually it is a bad idea, though, to allocate such a large variable on the stack, so you should use
mallocand friends to allocate it:This declares a pointer to a two-dimensional array.
Wenn passing your array to functions you can do similar, you’d just have to watch that the array bounds come first in the argument list, such that they are known when it comes to the array itself: