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 :
' vertices' undeclared (first use in this function).
Header file contains the following :
typedef struct
{
double x;
double y;
double z;
} variable;
In the main I call malloc and a function which will use this array of 'variable' called 'vertices':
int main (void)
{
variable *vertices = (variable*) malloc( 5000 * sizeof (variable) ) ;
load_file();
free(vertices);
return 0;
}
The function load_file ():
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 ", &vertices[j].x, &vertices[j].y, &vertices[j].z ) == 3 )
{
j++;
}
fclose(fp1);
In reality when I put the malloc in load_file, 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 'previous definition was here' and if I add variable vertices; before main, a get a load of errors.
How would I solve such an issue?
'vertices' undeclaredin load_file ??? that at least would make some sense.Variable) or a trailing '_t'variable_t