Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on what you have in your questions, if you are populating pA in a function I suspect you are not changing the contents of pA, which may be the issues. It may be helpful/useful to see the routine you are using to populate pA. </p> <p>C functions call by value if so you are just passing in a pointer (just pA), to change the value of the pointer, you need to pass the address of the pointer (pointer to a pointer if you will). Then you need to deference once it to set the pointer (to the pointer) and then deference it twice to set the values in allocated memory/array.</p> <p>So the calling to the function is </p> <pre><code>void init(File *fpin, double **pA, int lines) { /* Deference once to set the pointer to the pitner */ *pA = (double *)malloc(lines*sizeof(double)); for (int i=0; i&lt;lines; i++) { /* deference twice to set the the element of the pointer to the pointer */ fscanf(fpIn, "%lf", &amp;(**pA)[i]); } } </code></pre> <p>and then in calling the routine, you need to pass in the address of pA. </p> <pre><code>init(fpIn,&amp;pA,lines); </code></pre> <p>Here is a link which might help explain pointers to pointers: <a href="http://www.thegeekstuff.com/2012/01/advanced-c-pointers/" rel="nofollow">http://www.thegeekstuff.com/2012/01/advanced-c-pointers/</a></p> <p>It has been a long time since I have done pointers to pointers, but if you use the above, you may have to make some minor changes.</p> <p>As larsmans is hinting, I like his suggestion of changing the routine to return the pointer to the memory you allocated and place that value in pA. </p> <pre><code>double* init(File *fpin, int lines) { double *local = (double *)malloc(lines*sizeof(double)); //Fills Array for (int i=0; i&lt;lines; i++) { fscanf(fpIn, "%lf", &amp;local[i]); } return(local); </code></pre> <p>}</p> <p>and then in the calling to init</p> <pre><code>pA = init(fpIn,lines); </code></pre> <p>Depending upon what you are trying to accomplish, this may be easier to understand and not quite as complex.</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