For my project I had to save my data in a dynamic data structure (dynamic array, dynamic list etc..) which size and capacity would be set during execution time with malloc()
as I suppose. I completely forgot about it and I was working with that data without allocating any memory.
#include <stdio.h>
#include <stdlib.h>
typedef struct Bet {
char* bets[3][2];
} Bet;
int main() {
Bet betTypes = { .bets={{"Red", "Black"}, {"Even", "Odd"}, {"1 to 18", "19 to 36"}}};
}
This is what I am working with. I am confused how should I do this. Do I do
typedef struct Bet {
int x;
int y;
char* bets[x][y];
} Bet;
Then in main I create that Bet betTypes;
do betTypes.x = 3;
and betTypes.y = 2
and call malloc()
using those x and y? But then how do I create that exact same list of strings, because I didn't find any another ways aside Bet betTypes = { .bets={{"Red", "Black"}, {"Even", "Odd"}, {"1 to 18", "19 to 36"}}};
, if I declared Bet betTypes;
before that, then the Bet betTypes = { .bets={{"Red", "Black"}, {"Even", "Odd"}, {"1 to 18", "19 to 36"}}};
part would not work. Or I suppose I am just too unexperienced with memory allocation and I don't know how this should look like (neither I don't know how to allocate memory for this array)
question from:
https://stackoverflow.com/questions/65908917/allocating-memory-for-a-2-dimensional-array-that-is-in-a-structure 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…