I am implementing some lookup data in C99 (as a C-Script in the Software PLECS).
I want to create an array of struct with one (later this will be three) array members that will have a different (but always known) length for each struct. I would like to pass the data directly as I declare the struct array.
Here is what I tried so far.
#include <stdlib.h>
struct SomeStruct{
double a;
double *b;
};
struct SomeStruct StructArray[3] = {
[0].a = 1,
[0].b = (double*)malloc(2*sizeof(double)),
[0].b = {1.01, 2.01}, //obviously wrong
[1].a = 2,
[1].b = (double*)malloc(3 * sizeof(double)),
[1].b = { 1.01, 2.01, 3.01, }, //obviously wrong
[2].a = 3,
[2].b = (double*)malloc(4 * sizeof(double)),
[2].b = { 1.01, 2.01, 3.01, 4.01 }, //obviously wrong
};
Unfortunately my C is a little rusty and I cant figure out how to dereference the pointer inside the struct. *([N].b) does not work.
Is this even possible? Or is there maybe a more elegant soultion?
PS: The Data will be quite large, the struct array will have a length of about 200 and there will three arrays of up to length 400 in each of them. The code for this is generated as a header file from a MATLAB Script.
EDIT: Above has been solved by @Gilles, the following is still of interest
Here the alternative way, initializing at runtime. Why does the following bit not work?
#include <stdlib.h>
typedef struct {
double a;
double *b;
} SomeStruct;
SomeStruct StructArray[2]; //Sample struct
void initializeLUT() // Filling the Struct with Data
{
StructArray[0].a = 1;
StructArray[0].b = (double*)calloc(2, sizeof(double));
StructArray[0].b = { 1.01, 2.01 }; // does not work
StructArray[1].a = 2;
StructArray[1].b = (double*)calloc(3, sizeof(double));
*(StructArray[1].b) = { 1.01, 2.01, 3.01, }; //does not work either
}