如何在C中引用与全局变量共享同名的局部变量? [英] how refer to a local variable share same name of a global variable in C?
本文介绍了如何在C中引用与全局变量共享同名的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如
#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;
}
我觉得在函数栏中,直接调用foo只会得到全局的foo.我如何引用本地 foo?我知道在 C++ 中,有这个指针.但是,C 有类似的东西吗?
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?
非常感谢!
推荐答案
不,通过在 bar()
中声明 foo
,你已经采取了全局的 foo
超出范围.在 bar()
中,当你引用 foo
时,你会得到局部变量.
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.
这篇关于如何在C中引用与全局变量共享同名的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文