I want to be able to access a variable in one .c file that I declare in another .c file the first #includes the second). How do I declare it?
I want to be able to access a variable in one .c file that I declare in another .c file the first #includes the second). How do I declare it?
You declare it as extern in the various header files and/or source file and define it in exactly one source file.Originally Posted by Bladactania
You do know that it is usually a good idea to avoid global variables, right?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes... It's not a permanent solution, just a temporary one.
One question though, is it ok to use a "global" variable within one source file?
Maybe, in the same sense that it may be okay to use a global variable across different source files. Why do you need to use the global variable in the first place? A temporary solution can become a permanent solution that might actually be problematic in the future.Originally Posted by Bladactania
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well, I'm in the process of splitting a large source file into smaller source files to make it more organized. The original programmer used a large number of global scope variables that were used by the individual functions. Once I split them up, I want to make sure that I've cut/paste them correctly before I start making changes to the functions (adding parameters) so I know where any errors are coming from. So I want to make his globals global across the entire project for this reason.
As far as using globals within one source file, I generally prefer to declar my constants at the very top of the source for easy editing. I suppose I could declare them within main.
Oh, but global constants are not global variables. They are far less problematic to trace than global variables since they do not add state to the program (you can be pretty sure that the constant has the particular value.)Originally Posted by Bladactania
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I see. So global constants ok, global variables bad...
Can you pass by reference in C or must I pass pointers?
No passing by reference in the language itself; pointers are used to simulate pass by reference.Originally Posted by Bladactania
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Once your code grows and you start referencing this global variable all over the place, you'll find it becomes a lot less temporary
As usual, not preferred, but if you must use a file scope variable in a single module at least make it static.One question though, is it ok to use a "global" variable within one source file?
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
What I usually use global variables for is when I have several small functions that are only a few lines long that all use the same variables. It doesn't sit well with me when the function declaration is longer than the function itself.
The usual cure against having a lot of parameters is to keep related variables together in a struct.
However, it is good to know (at the place of call) that these function work with the data of this or that employee, rather than limiting the usage only to some globally visible ones. If you know that the function is correct and it doesn't rely on globals, nothing you do later with other code breaks this function. You know what goes in and what will come out since it is tested (and it is easier to ensure that each single function in isolation is OK than to ensure that for a program where every part depends on every other part). Once tested, you can always just step over it in the debugger.Code:typedef struct { char* first_name; char* last_name; int id; } Employee; void do_something(Employee* emp); void interact(Employee* first, Employee* second); //vs. void do_something(char* first_name, char* last_name, int id); void interact(char* first_name_a, char* last_name_a, int id_a, char* first_name_b, char* last_name_b, int id_b);
Last edited by anon; 02-11-2009 at 12:54 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
You know, that's a good idea! Thanks!
I hear that! At work we're creating a new front-end to an already existing program with absolutely horrible architecture. But because we were specifically told to keep the design the same, our code is hideous...I'm in the procesws of editing someone elses code