I have been searching for a solution for this for a while now. I think I know what is happening and what the solution should be, however I am not quite sure how to implement it.
I have a struct that contains two variable length arrays. These are to be filled within a function and returned to the calling function to do work with. The problem seems to be that any assignment of the variable length array becomes invalid when the called function goes out of scope. I would guess that a solution may be to allocate the memory on the heap and then free the memory once I am done with the struct in the calling function. A code example is given below
struct fields {
int n;
double * A;
double * B;
};
struct fields field() {
int n = 4;
double A[n] = { 1, 2, 3, 4 };
double B[n] = { 1, 2, 3, 4 };
struct fields field;
field.n = n;
field.A = A;
field.B = B;
/* field can be accessed with n, A, B set properly */
return field;
}
double calling_function() {
struct fields field1 = field();
/* field1 contains n but A and B have not returned */
.
.
.
}