如何访问正确的全局变量在C 2 [英] how to access the correct global variable in C?

查看:134
本文介绍了如何访问正确的全局变量在C 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一些全局变量main.c中GLOBAL,但我的main.c中有一个的#includeother.h。但other.h具有全局变量GLOBAL了。

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 GLOBAL 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?

推荐答案

我假设你使用的#define定义的程序中的变量,而不是在preprocessor。

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

如果您想引用在main.c中创建的,你只需要输入全球。指代那些在你的头文件,使用关键字的extern。

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.

这篇关于如何访问正确的全局变量在C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆