开发者

how to access the correct global variable in C?

lets say I have some global var GLOBAL in main.c, but my main.c has a #include "other.h". But other.h has global var GLOB开发者_开发百科AL too.

How do I let the compiler know which one I meant when I write GLOBAL in main. Is there a "this" keyword that I can use?


You can't have two global variables with the same name in C program. C might allow multiple definitions in the same file scope through the tentative definition rule, but in any case all definitions will refer to the same variable. So, apparently your question is based on incorrect premise. There's no issue of "which one" here. You only have one variable.

For example, you can have a sequence of file-scope definitions in C translation unit

int i;
int i;
int i;

This is legal in C (as opposed to C++) and all these definitions are actually defining the same variable, not three different variables.

Of course, if one of the variables is defined as a local variable (if so, you have to state it in your question) (BTW, why is it called GLOBAL then?), then it will hide the name of the variable defined in the file scope (or any higher scope). In this case there's no way to access the hidden name in C. Rename the local variable to avoid hiding the global one.

What you mean by "other.h also has the variable" is not clear as well. What does "has" mean in this case? Is the variable defined in other.h? Or just declared? If it is just declared, then it doesn't really "have" it. If it is defined there... well, then the real question is: why are you defining variables in .h files?


You don't actually have two variables. There is only one, you will either get a compiler (or linker) error if two modules declare the same global with the same name, or the compiler/linker will decide that you meant this to be redundant declarations of a single variable and merge them.


Like others have mentioned, avoid using the same global variable/function names. Take the habit of prefixing them with the module name. I.e. MODULE1_state, MODULE2_state, etc.

If a global variable is only going to be used inside one source file, don't declare it in the matching header file. Instead, declare it at the top of the source file, and use the static keyword. Variables that need to be accessible to other modules need to be declared in the header file using the extern keyword. The same applies to global functions. It helps to maintain the same public/private discipline you'd normally use in an object-oriented language like C++, Java, C#, etc.

Example:

In module.h:

#ifndef MODULE_H /* Header guard */
#define MODULE_H

/* Declarations for entities that CAN be accessed by other modules,
   i.e. "public". */
extern int MOD_publicVariable;
extern void MOD_publicFunction(int arg);

#endif // MODULE_H

In module.c:

/* Definitions for entities that CAN be accessed by other modules,
   i.e. "public". */
int MOD_publicVariable = 42;
void MOD_publicFunction(int arg) {...}

/* Declarations for entities that CAN'T be accessed by other modules,
   i.e. "private". */
static double MOD_privateVariable = 12.34;
static void MOD_privateFunction1(void);
static void MOD_privateFunction2(void);

/* Function definitions. */
void MOD_privateFunction1(void) {
     int localVariable; /* This doesn't need the module prefix. */
     ....
}

void MOD_privateFunction2(void) {
     ....
}

The module prefix (MOD_) can be named directly after your module, or you can use an abbreviation. Experiment, and you'll eventually settle on a convention you like. Consistently using a prefix like this emulates the concept of a class/module/namespace in OO languages.

Make sure you know the difference between declaration vs. definition, and extern vs. static.

It irks me that C textbooks and courses either ignore or gloss over the art of multi-module programming.

EDIT: I forgot to mention that you should not generally make global variables accessible to other modules (i.e. make them "private"). If other modules need to access that variable, provide "public" "setter" and/or "getter" functions.


You can have only one definition per object module. The second one, if has the same content, will be ignored. If it differs, it will result in compiler error.


This might not be the answer you are looking for, but why don't you try to avoid this kind of situations in the first place (and potentially the (over)use of globals)?


First off, if this is an issue in the first place, you're using a crappy library and should rewrite/switch if possible. If you can't, you can do something like this:

other.h:

int GLOBAL;
//...other stuff

main.c

int GLOBAL;
#define GLOBAL OTHER_GLOBAL
#include "other.h"
#undef GLOBAL

int main(int argc,char** argv)
    {
    printf("%i %i",GLOBAL,OTHER_GLOBAL);
    getchar();
    return 0;
    }

however, if as the capitals imply, GLOBAL is #defineed, this might not work. (But it's worth a try anyway.)


I'm assuming you've defined the variable in the program, and not in the preprocessor using a #define.

If you want to refer to the one created in the main.c, you just type global. To refer to those in your header file, use the keyword extern.

You'd be better declaring them as two seperate variable names though to be honest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜