Joh*_*itb 43
如果您的文件范围变量不是静态的,那么您可以使用在嵌套范围中使用extern的声明:
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\n", c);
}
// outputs 1
printf("%d\n", c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果使用static声明变量,我看不到引用它的方法.
Dut*_*tow 21
没有:: in c但你可以使用getter函数
#include <stdio.h>
int L=3;
inline int getL()
{
return L;
}
int main();
{
int L = 5;
printf("%d, %d", L, getL());
}
Run Code Online (Sandbox Code Playgroud)
如果您谈论的是阴影dlsym()全局变量,那么(在 Linux 上)您可以使用它来查找全局变量的地址,如下所示:
int myvar = 5; // global
{
int myvar = 6; // local var shadows global
int *pglob_myvar = (int *)dlsym(RTLD_NEXT, "myvar");
printf("Local: %d, global: %d\n", myvar, *pglob_myvar);
}
Run Code Online (Sandbox Code Playgroud)
如果你想让你的代码看起来很性感,请使用宏:
#define GLOBAL_ADDR(a,b) b =(typeof(b))dlsym(RTLD_NEXT, #a)
...
int *pglob_myvar;
GLOBAL_ADDR(myvar, pglob_myvar);
...
Run Code Online (Sandbox Code Playgroud)