如何引用局部变量在C*享全局变量的同名? [重复]

时间:2022-03-29 09:34:21

This question already has an answer here:

这个问题在这里已有答案:

for example

#include<stdio.h>

int foo = 100;

int bar()
{
    int foo;
    /* local foo = global foo, how to implemented? */
    return 0;
}

int main()
{
    int result = bar();
    return 0;
}

I think in the function bar, calling foo directly will just get the global foo. How can I refer the local foo? I know in C++, there is this pointer. However, does C has something similar?

我认为在功能栏中,直接调用foo将获得全局foo。我怎么能引用当地的foo?我知道在C ++中,有这个指针。但是,C有类似的东西吗?

Thanks a lot!

非常感谢!

1 个解决方案

#1


13  

No, by declaring foo in bar(), you have taken the global foo out of scope. Inside bar() when you refer to foo you get the local variable.

不,通过在bar()中声明foo,你已经将全局foo从范围中删除了。当你引用foo时,在bar()里面你得到了局部变量。

#1


13  

No, by declaring foo in bar(), you have taken the global foo out of scope. Inside bar() when you refer to foo you get the local variable.

不,通过在bar()中声明foo,你已经将全局foo从范围中删除了。当你引用foo时,在bar()里面你得到了局部变量。