Here is a simplified version of two structs I have:
struct MyStruct1 {
double d;
}
struct MyStruct2 {
struct MyStruct1* a;
int i;
}
I can initialize the second struct as follows:
void InitStruct(struct MyStruct2 pMyStruct2) {
static struct MyStruct1 A[] = { {.d=12} , {.d=17} , {.d=1} };
*pMyStruct2 = (struct MyStruct2) { .a = A, .i = 3 };
}
but actually I have to initialize it this way (its because this struct is again part of a bigger structure that shall be initialized at once):
void InitStruct(struct MyStruct2 pMyStruct2) {
*pMyStruct2 = (struct MyStruct2) {
.a = (struct MyStruct1[]) {
{.d=12} , {.d=17} , {.d=1}},
.i=3 };
}
Both ways compile without any warnings but the data in the second solution gets corrupted.
I think that the inner array is not static and thus the .a-pointer gets invalid immediately.
Is there another way to tell the compiler to keep the data of the array in the memory?
staticrefers to linkage, not to storage duration. Are you sure you're not simplifying something important away ?bbeing declared? Can you post a small program that exhibits the problem?