This question already has an answer here:
- How do I correctly set up, access, and free a multidimensional array in C? 5 answers
So I have a program with a struct
typedef struct s_struct {
int rows;
int cols;
char* two_d; //This is supposed to be the 2D array
} *GRID;
I want to create a struck and dynamically allocate memory to it and then fill the 2D array but I don't know how. Here is what I have for the create(int prows, int pcols) function:
GRID grid = malloc(sizeof(struct s_struct));
grid ->rows = prows;
grid ->cols = pcols;
grid ->two_d = malloc(sizeof(char) * (rows*cols));
I don't understand how this creates a 2D array if it even does and how I can go about filling the array.