Note that there are some explanatory texts on larger screens.

plurals
  1. POArray Passing in C
    primarykey
    data
    text
    <p>I'm having a problem passing an array to a function in C. I declare the array in main</p> <p>And then, inside of a function, I populate it.</p> <p>At which point I leave my function and pass the array to another function with the prototype</p> <p>And make an attempt to access the array.</p> <p>However once I am out of the initial function that populated the array, I am unable to access the array. It just returns values of 0.00000 and nan.</p> <pre><code>void ReadData(FILE *fpIn, int lines, double *pA ); void MinMaxAvg(double *pA, double *min, double *max, double *avg, int lines); int main() { FILE *fpIn = NULL; int lines = 0; double *pA = NULL; ReadData(fpIn, lines, pA); double min = 0; double max = 0; double avg = 0; MinMaxAvg(pA, &amp;min, &amp;max, &amp;avg, lines); printf("%lf %s %lf %s %lf \n", avg, " ", min, " ", max); return 0; } void ReadData(FILE *fpIn, int lines, double *pA ) { char fName[20]; scanf( "%s", fName); fpIn = fopen ( fName, "rt"); // fpIn = fopen( "test1.txt", "rt"); if ( fpIn == NULL) { printf( "Unable to open: "); exit(99); } //Gets Lines //int lines=0; char ch; while((ch=fgetc(fpIn))!=EOF) { if (ch=='\n') { lines++; } } // clearerr(fName *fpIn); fclose(fpIn); fopen(fName, "rt"); //Makes Array //double *pA; pA = (double *)malloc(lines*sizeof(double)); //Fills Array for (int i=0; i&lt;lines; i++) { fscanf(fpIn, "%lf", &amp;pA[i]); } for (int i=0; i&lt;lines; i++) { printf("%lf \n", pA[i]); } } void MinMaxAvg(double *pA, double *min, double *max, double *avg, int lines) { double total = 0; for (int i=0; i&lt;lines; i++) { if ( i==0) { *min = pA[0]; *max = pA[0]; } else { if (pA[i] &lt; *min) { *min = pA[i]; } if (pA[i] &gt; *max) { *max = pA[i]; } } total += pA[i]; } *avg = (total / (double)lines); } </code></pre> <p>Any Suggestions?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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