Say that I have the following arrays that I call in a function:
int n = 20;
double x[] = {
0.003435700407453, 0.018014036361043, 0.043882785874337, 0.080441514088891,
0.126834046769925, 0.181973159636742, 0.244566499024586, 0.313146955642290,
0.386107074429177, 0.461736739433251, 0.538263260566749, 0.613892925570823,
0.686853044357710, 0.755433500975414, 0.818026840363258, 0.873165953230075,
0.919558485911109, 0.956117214125663, 0.981985963638957, 0.996564299592547
};
double w[] = {
0.008807003569576, 0.020300714900193, 0.031336024167055, 0.041638370788352,
0.050965059908620, 0.059097265980759, 0.065844319224588, 0.071048054659191,
0.074586493236302, 0.076376693565363, 0.076376693565363, 0.074586493236302,
0.071048054659191, 0.065844319224588, 0.059097265980759, 0.050965059908620,
0.041638370788352, 0.031336024167055, 0.020300714900193, 0.008807003569576
};
I would like to return the int n and the two arrays. I can do this by using a structure which is easy if I know the length of array x and w. However, the function depending on the inputs can return an array x of length 2,4,6,15, etc and an array w of length 2,4,6,15, etc. I do not know the length of array w and x.
I've created a structure:
struct quadpts{ //structure used to pass multiple values into roeFlux
int n; //The specific heat ratio
double *x;
double *w;
};
typedef struct quadpts Quadpts;
and used:
Quadpts qpt = (Quadpts) malloc(size(Quadpts));
to assign the varied length values in array x and w. However, I realized that people were adding one value at a time in many of the examples I have seen and looked up. Is it possible to assign a whole array to a varied length array in a structure? Or am I limited to filling the array in the structure 1 by one. Can this be done using a for loop? If so, would I have to define
Quadpts qpt = (Quadpts) malloc(size(Quadpts));
in a different way each time to account for the new addition to the structure array?
Thank you for your help. I'm new at C and am limited to MATLAB knowledge.
EDIT:
I realized that I have problems with those big arrays. I'm using a switch case syntax in order to allocate different size arrays to the variable x and w. But I realized that I need to designate the size of those arrays to begin with and they will only be seen within the for loop. How can I make it such that the arrays are seen outside of for loops such that I can save them to the structures? The following is a shorten version of my code. I'm constantly getting error at double x[n] as previous definition was here or redefinition.
double quad1d(int pqOrder){
int n;
switch(pqOrder)
{
case 1:
n = 1;
double x[n] = {
0.500000000000000
};
double w[n] = {
1.000000000000000};
break;
case 3:
// Order 3 Gauss-Legendre points
n = 2;
double x[n] = {
0.211324865405187, 0.788675134594813
};
double w[n] = {
0.500000000000000, 0.500000000000000
};
break;
}
int i;
Quadpts * qpt = (Quadpts*)malloc(sizeof(Quadpts));
for (i=0; i<n; i++){
qpt->x = malloc(qpt->x_len * sizeof(double));
qpt->w = malloc(qpt->w_len * sizeof(double));
qpt.x=x[i];
qpt.w=w[i];
}
return &(qpt[0]);
}