Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable declaration
    text
    copied!<p>I am working a piece of code which reads data from a file and manipulates it. The idea is to load the data in a global manner and then use several functions on the data to perform calculations. The problem I am having is that when compile I get the following error :</p> <blockquote> <p>' vertices' undeclared (first use in this function).</p> </blockquote> <p>Header file contains the following :</p> <pre><code>typedef struct { double x; double y; double z; } variable; </code></pre> <p>In the main I call malloc and a function which will use this array of <strong>'variable'</strong> called 'vertices':</p> <pre><code>int main (void) { variable *vertices = (variable*) malloc( 5000 * sizeof (variable) ) ; load_file(); free(vertices); return 0; } </code></pre> <p>The function load_file ():</p> <pre><code> FILE *fp1 ; fp1 = fopen( 'file',"r"); if (fp1==NULL) { printf("File couldn't be opened or read!"); return 1; } int j = 0; while(fscanf(fp1, "%lf %lf %lf ", &amp;vertices[j].x, &amp;vertices[j].y, &amp;vertices[j].z ) == 3 ) { j++; } fclose(fp1); </code></pre> <p>In reality when I put the malloc in <strong>load_file</strong>, it compiles and works but the problem is that I have various other functions which will use the data and if I free it in load_file, I lose everything. If I redefine the typedef above main, I get a <strong>'previous definition was here'</strong> and if I add <strong>variable vertices;</strong> before main, a get a load of errors.</p> <p>How would I solve such an issue? </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