I have a struct in C, which members are float arrays. I want to initialize it during compile time like this:
typedef struct curve {
float *xs;
float *ys;
int n;
} curve;
curve mycurve1 = {
{1, 2, 3},
{4, 2, 9},
3
};
curve mycurve2 = {
{1, 2, 3, 4},
{0, 0.3, 0.9, 1.5},
4
};
But I get compile errors.
One possible solution might be to use arrays and not pointers in the struct. This is the accepted answer of a very similar question from here: https://mcmap.net/q/571278/-declaring-int-array-inside-struct, but the problem with that approach is that I don't know the array size at typedef time. Not only that, I might want to initialize another, bigger curve.
Another approach might be with malloc, but I find that overkill, because I know the array size at compile time and I don't need it to change during run-time.
I don't know another approaches, which might be useful. Maybe casting array to pointer?? - I don't really know how I would approach that.
const
withconst curve mycurve1 = { ...
– Oconnerconst
keyword do? (besides not allowing any changes to that array). Is is essentially a compiler hint for optimization? Or does it do someting else? Why should I consider it? – Terminable