简体   繁体   中英

Variable declaration

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?

The problem is that you're declaring "vertices" inside main which makes its scope local to main and load_file() can't see it.

Change the declaration of load_file() as follows...

void load_file( variable* vertices )
{
    /// blah blah
}

and then in main, pass your vertices variable in to it...

int main (void)
{   
    variable *vertices = malloc( 5000 * sizeof (variable) ) ; 
    load_file( vertices );
    free(vertices);
    return 0;
}

EDIT: I would recommend against just making vertices a global variable... that means that anything can access it and/or modify it... even unintentionally. It's almost always wiser to pass arguments in and out of the functions that need them rather than to just make them globally available to the world... scope is your friend.

If you want the vertices to be accessible by all of the functions in the file, then move its declaration outside of the main() function (ie, give it global scope) and only initialize it inside of the main() function:

#include <stdlib.h>
#include <stdio.h>

typedef struct {
    double x;
    double y;
    double z;
} variable;

static variable *vertices = NULL;

int 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);
    return 0;
}

int main (void){
    vertices = (variable*) malloc( 5000 * sizeof (variable) ) ;
    load_file();
    free(vertices);
    return 0;
}

If you want the vertices pointer to accessible across all files in your program, then give it external linkage by removing the static keyword from its declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM