Advertisement

Global Variables

Started by November 21, 2000 02:58 PM
0 comments, last by Tsu 24 years, 2 months ago
As the name (of the post) implies, i was wondering if it is possible to, using c/c++ and multiple "c" files, to set global variables that can be used and modified by any other function or procedure in any other "c" file...??? i managed to get it to work with one "c" file and a globals.h file, by putting the var declarations in the globals.h file. this worked, but whats the point when you have one "c" file??? i then tried it with about 6 "c" files and it gave me about 120 errors stating that those variables were already declared (it included the .h code in the .c files... go figure...) sorry if this seams to be a stupid question, i''m still a little new to C... (but enthusiastically learning quickly ) also, while i''m at it, i''m also wondering if using Allegro (for djgpp) is worth it... i want to program games and such, and its all i have (for windows anyway... i do have a bunch of linux lirary-thingees, should i learn those instead?)... by the way, when i say "c" files, i mean the source files... as in .c / .cpp / .whateverelsethereis _________
Tsutomegi
in your main "c file" declare your global variable like you normally would

myVarType myVar;

then in your other "c files" that you want to use the global, declare it like this

extern myVarType myVar;

now whatever you do to that varabile in one file it will take effect in all the other files
be sure you have it declared as the same type and as the same variable name


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

This topic is closed to new replies.

Advertisement