如何在 C 中访问隐藏的全局变量? [英] How can I access a shadowed global variable in C?
本文介绍了如何在 C 中访问隐藏的全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 C 中访问隐藏的全局变量?在 C++ 中,我可以使用 ::
作为全局命名空间.
How can I access a shadowed global variable in C? In C++ I can use ::
for the global namespace.
推荐答案
如果您的文件范围变量不是静态的,那么您可以使用在嵌套范围内使用 extern 的声明:
If your file-scope variable is not static, then you can use a declaration that uses extern in a nested scope:
int c;
int main() {
{
int c = 0;
// now, c shadows ::c. just re-declare ::c in a
// nested scope:
{
extern int c;
c = 1;
}
// outputs 0
printf("%d
", c);
}
// outputs 1
printf("%d
", c);
return 0;
}
如果变量是用静态声明的,我看不到引用它的方法.
If the variable is declared with static, i don't see a way to refer to it.
这篇关于如何在 C 中访问隐藏的全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文