1

My intention is to create a multidimensional array of structs. However, the problem is that it needs to be a global variable, but I don't know the size at compile time (it is user input).

Lets say I have a Struct like so:

typedef struct abc abc;
struct abc {
  int test;
  char* color;
};

I need to declare a global multidimensional array for this.

abc** board;

Within a function later in the code, I then want to initialise the size and fill it with values:

board[height][width];

for(int i = 0; i < height; i++) {
    for(int x = 0; x <width; x++) {
        board[i][x].test = 0;
    }
}

Where am I going wrong? It seem's this part is the issue: board[height][width] ?

Alex
  • 49
  • 7

2 Answers2

0

Answered by @xing

malloc could be used to allocate memory to board and it could be accessed using board[x][y] notation

board = malloc ( height * sizeof ( *board)); then in a loop allocate memory as board[i] = malloc ( width * sizeof ( **board));. the structure also has a char * that will need to be allocated

Alex
  • 49
  • 7
0

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)

JeremyP
  • 84,577
  • 15
  • 123
  • 161