0

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?

5
  • So which line gives the error? Commented Mar 21, 2013 at 19:50
  • Why did you call a type "variable"? This makes no sense. Commented Mar 21, 2013 at 19:52
  • Are you sure it doesn't say 'vertices' undeclared in load_file ??? that at least would make some sense. Commented Mar 21, 2013 at 19:53
  • @Roddy You are absolutely right it does give that error. I just mixed them up. Commented Mar 21, 2013 at 20:00
  • I've edited your post. @KScottPiel's answer looks right to me. In future it's a great idea to have a naming convention that makes distinguishing types from variables easy. Most people either give types a leading capital (Variable) or a trailing '_t' variable_t Commented Mar 21, 2013 at 20:09

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If this is indeed C and not C++, then don't cast malloc.
True... I should have edited that out when I copy/pasted his code. Good catch.
1

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 (i.e., 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.

2 Comments

I had the exact same code as above apart from the null pointer definition : variable *vertices = NULL;. Now it appears to be running (with a segmentation fault...). Thank you very much Sir.
i'd recommend the suggestion of @K Scott Piel. global variables should be reserved for constants. any function that needs the vertices variable should be explicitly given it as a parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.