如何访问在C阴影全局变量? [英] How can I access a shadowed global variable in C?

查看:135
本文介绍了如何访问在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\n", c);
    }
    // outputs 1
    printf("%d\n", c);
    return 0;
}

如果变量声明为静态,我没有看到一个方法来参考一下吧。

If the variable is declared with static, i don't see a way to refer to it.

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

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