There are two ways of doing this. The first stage is the same in both cases: allocate an array of pointers to abc (assume the user has already typed values for height and width and you have validated them).
board = calloc(height, sizeof *board);
Now you have an array of height
null pointers (purists would say this is not guaranteed by the C standard, but we'll ignore them because it doesn't matter).
Now, either you allocate height
arrays of width
structs
for (size_t i = 0 ; i < height ; ++i)
{
board[i] = calloc(width, sizeof **board);
}
Or you alllocate one enormous array for the whole thing and set the pointers in board to the right offsets.
abc *temp = calloc(width * height, sizeof *board);
for (size_t i = 0 ; i < height ; ++i)
{
board[i] = &temp[i * width];
}
The use of calloc
means that each struct will already be initialised with a zero bit pattern which means that on most platforms, test
will be 0 and color
will be a null pointer for every single abc
in the array.
The second option is a bit more opaque but more efficient if you need to do the allocation a lot or if height
is likely to be large. It's also easier to deallocate requiring only two calls to free()
.
On the subject of freeing, if this global array is allocated once and has a life time of the entire program, you don't need to bother about freeing it since all the memory will be returned to the OS when the program terminates anyway (unless you are on an embedded platform or a retro environment without virtual memory)