Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding double type arrays to a structure of undefined length arrays
    text
    copied!<p>Say that I have the following arrays that I call in a function:</p> <pre><code> 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 }; </code></pre> <p>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. </p> <p>I've created a structure:</p> <pre><code> struct quadpts{ //structure used to pass multiple values into roeFlux int n; //The specific heat ratio double *x; double *w; }; typedef struct quadpts Quadpts; </code></pre> <p>and used:</p> <pre><code>Quadpts qpt = (Quadpts) malloc(size(Quadpts)); </code></pre> <p>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 </p> <pre><code>Quadpts qpt = (Quadpts) malloc(size(Quadpts)); </code></pre> <p>in a different way each time to account for the new addition to the structure array?</p> <p>Thank you for your help. I'm new at C and am limited to MATLAB knowledge. </p> <p>EDIT:</p> <p>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. </p> <pre><code>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&lt;n; i++){ qpt-&gt;x = malloc(qpt-&gt;x_len * sizeof(double)); qpt-&gt;w = malloc(qpt-&gt;w_len * sizeof(double)); qpt.x=x[i]; qpt.w=w[i]; } return &amp;(qpt[0]); </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload