问题
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?
Thanks a lot!
回答1:
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.
来源:https://stackoverflow.com/questions/5827447/how-refer-to-a-local-variable-share-same-name-of-a-global-variable-in-c