This question already has an answer here:
这个问题在这里已有答案:
- How to print value of global variable and local variable having same name? 4 answers
如何打印具有相同名称的全局变量和局部变量的值? 4个答案
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()里面你得到了局部变量。