【问题标题】:How I can access a hidden variable in C [duplicate]我如何访问 C 中的隐藏变量 [重复]
【发布时间】:2011-11-21 10:13:20
【问题描述】:

可能重复:
How can I access a shadowed global variable in C?

在 C++ 中,我可以使用 :: 运算符来指定全局变量。例如:

using namespace std;
int foo = 10;
int main(){
     int foo = 5;
     cout<<" Global variable: "<< ::foo <<endl;
     cout<<" Local Variable: " << foo <<endl;
     return 0;
}

如何在 C 中做到这一点?

【问题讨论】:

    标签: c scope


    【解决方案1】:

    这是an earlier question 的主题。可以如下实现

    int foo = 10;
    int main(void) {
         int foo = 5;
         {
             extern int foo;
             foo++;
         }
         foo++
         return 0;
    }
    

    但是在实践中,我无法想象会遇到这个问题,因为我总是可以重命名局部变量或创建一个小的 static inline 函数来访问全局 foo 并且我可以调用它。

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多