Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

scope - How can I access a shadowed global variable in C?

How can I access a shadowed global variable in C? In C++ I can use :: for the global namespace.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...