1

I have problem with a static variable.

I tried to make maze program by using stack.

At first, it activates currently when I put all codes in same source file.

But after I separate main to main.c source, and other functions to function.c, an error occurred at static variable.

This is part of code in function.c file that problem happen.

I used EXIT_ROW and EXIT_COL as static variable, and these initialized at main function. And I use EXIT_ROW and EXIT_COLS at other function.c file but when I do debugging this file, EXIT_ROW and EXIT_COL didn't initialize at all.

void main() { int xsize, ysize; FILE *fp; if( !( fp = fopen("input.txt", "r") ) ) { fprintf(stderr, "FILE couldn't open\n"); exit(EXIT_FAILURE); }; fscanf(fp, "%d %d", &ysize, &xsize); EXIT_ROW = ysize; EXIT_COL = xsize; printf("%d %d\n", ysize, xsize); init_maze(xsize, ysize, fp); print_maze(xsize, ysize); path(); } 

I couldn't understand why it happened.. EXIT_ROW and EXIT_COLS are declared in stack.h header file. can u help me why it happened, and how can I fix it?

4
  • 1
    Please post a minimal complete and verifiable example. In particular, since you are asking about EXIT_ROW and EXIT_COLS you need to show exactly how and where that is defined. But if they are indeed declared as global static then of course they can only be accessed within each file. That is what the static keyword does. So in fact you would have two independent versions of each of those variables - one set for each file. Commented Mar 28, 2016 at 11:10
  • Thank you for your answer. so, you mean, static only in same sorce file could access to static keyword? not in same projects?? Commented Mar 28, 2016 at 11:15
  • 1
    Yep. static can mean two things: on "global level", it says "only to be known in this compilation unit" (Your case, apparently. We could say definitely, if you had shown the variable definition and where it is. Within functional scope, static means a different thing. Commented Mar 28, 2016 at 11:18
  • 1
    @ppappiya. Yes. What you really want is a non-static global variable. Or a static variable with accessor APIs. For the non-static variable be sure to define it only once. That is, the actual definition should go in only one .c file, not in the header file. The header file should only contain an extern declaration of those variables. Commented Mar 28, 2016 at 11:22

1 Answer 1

1

I can assume that you defined the variables with the internal linkage and with file scopes. So each translation unit has its own set of these varaibles.

Any initialization of these variables in one translation unit does not influence on the variables in other translation unit.

Remove keyword static in the declaration of the variables. Declare them in some header with keyword extern and define them only in one translation unit.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.