'how can i have a struct have an unknown size array member?
im trying to make a sort of minesweeper clone in c, but i cant figure out how to create a variable length array member of a struct and initialize it.
my struct definitions
typedef struct Cell
{
Rectangle area; // The area the cell covers
bool hasMine; // If the cell covers a mine
bool covered; // If the cell is currently covered
int touching; // How many mines the cell is touching
} Cell;
typedef struct Minefield
{
int columns; // The amount of columns
int rows; // The amount of rows
int mines; // The amount of mines
Cell field[]; // An array of columns*rows Cells
} Minefield;
how im creating new instances of a Minefield
Minefield NewGame(int columns, int rows, int mines)
{
int i = 0;
Cell cells[columns*rows];
for (i=0; i < columns*rows; i++) {
int x = 0;
int y = 0;
if (i == 0) {
Rectangle area = (Rectangle){0, 0, cellsize, cellsize};
cells[i] = (Cell){area, false, true, 0};
}
else {
Cell previous = cells[i-1];
x = previous.area.x + cellsize;
y = previous.area.y + cellsize;
Rectangle area = (Rectangle){x, y, cellsize, cellsize};
cells[i] = (Cell){area, false, true, 0};
}
}
Minefield field = (Minefield){columns, rows, mines, cells};
return field;
}
im new to c, ive done some googling and ive come across some very similar questions, but i cant really understand them or the examples they give. i think you can do what my errendous code is trying to do (or something very similar) with a gcc extension, but i want my code to be portable. how can i have a struct have an unknown size array member?
Solution 1:[1]
Unfortunately, C doesn't have any built-in dynamic array like python. However, if you want to use some dynamic array either you can implement your self or you can see the answer to that link C dynamically growing array which explains that you need to have starting array with a default size, and then you can increase the size of the array which depends on your algorithm. After you import that header file provided by @casablance, you can simply use it for your minesweeper game.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BGForDevelopers |
