I am trying to create a struct with a variable length array of fixed size strings.
struct foo_query{
char tag[10];
int value_count;
char * values[VAL_SIZE];
};
Now, I want to create an array of these structs, and allocate some memory for the values:
#define buffer(a) (char *) malloc(sizeof(char[a][VAL_SIZE]))
foo_query queries[total_queries] = {
{"FOO", 25, buffer(25)},
{"BAR", 21, buffer(21)}
};
#undef buffer
Finally, I want to actually write some data to the values.
query_index = 0;
for(int i = 0; i < queries.value_count; i++){
strncpy(queries[query_index].values[i], "Hello", VAL_SIZE);
Serial.outln("success");
}
But that last bit fails. success
is printed once or twice, and then everything just stops.
As far as I can tell, the memory has been allocated and I'm not overflowing anything, so why is the code crashing?