找到你要的答案

Q:How can I access a shadowed global variable in C?

Q:我怎么能访问蒙上阴影的全局变量在C?

How can I access a shadowed global variable in C? In C++ I can use :: for the global namespace.

我怎么能访问蒙上阴影的全局变量在C? In C++ I can use :: for the global namespace.

answer1: 回答1:

If your file-scope variable is not static, then you can use a declaration that uses extern in a nested scope:

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;
}

If the variable is declared with static, i don't see a way to refer to it.

如果你的文件作用域变量不是静态的,那么你可以使用声明使用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;
}

如果变量是静态声明的,我看不出有什么方法可以引用它。

answer2: 回答2:

There is no :: in c but you can use a getter function

#include <stdio.h>

int L=3;

inline int getL()
{
   return L;
}

int main();
{
   int L = 5;

   printf("%d, %d", L, getL());
}

没有::C但是你可以使用getter函数

#include <stdio.h>

int L=3;

inline int getL()
{
   return L;
}

int main();
{
   int L = 5;

   printf("%d, %d", L, getL());
}
answer3: 回答3:

If you are talking about shadowed global var, then (on Linux) you can use dlsym() to find an address of the global variable, like this:

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);
}

If you want your code to look sexy, use macro:

#define GLOBAL_ADDR(a,b)  b =(typeof(b))dlsym(RTLD_NEXT, #a)
...
int *pglob_myvar;
GLOBAL_ADDR(myvar, pglob_myvar);
...

如果你谈论的是阴影的全局变量,然后(Linux)可以使用dlsym()找到全局变量的地址,这样:

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);
}

如果您希望您的代码看起来性感,请使用宏:

#define GLOBAL_ADDR(a,b)  b =(typeof(b))dlsym(RTLD_NEXT, #a)
...
int *pglob_myvar;
GLOBAL_ADDR(myvar, pglob_myvar);
...
answer4: 回答4:

Depending on what you call shielded global variable in C, different answers are possible.

If you mean a global variable defined in another source file or a linked library, you only have to declare it again with the extern prefix:

extern int aGlobalDefinedElsewhere;

If you mean a global variable shadowed (or eclipsed, choose the terminology you prefer) by a local variable of the same name), there is no builtin way to do this in C. So you have either not to do it or to work around it. Possible solutions are:

  • getter/setter functions for accessing global variable (which is a good practice, in particular in multithreaded situations)

  • aliases to globals by way of a pointer defined before the local variable:

    int noName;
    {
        int * aliasToNoName = &noName; /* reference to global */
        int noName;                    /* declaration of local */
        *aliasToNoName = noName;       /* assign local to global */
    }
    

根据你所说的屏蔽全局变量C,不同的答案是可能的。

如果你是一个全局变量,在另一个源文件或链接库定义的,你只需要声明了extern前缀:

extern int aGlobalDefinedElsewhere;

如果你是一个全局变量(或阴影遮挡,选择您喜欢的术语)的同名的局部变量),没有内置的方法来做这件事在C.,那么你就可以不去做或围绕它的工作。可能的解决方案是:

  • 访问全局变量的getter和setter函数(特别是在多线程的情况下,是一个很好的做法,)

  • 别名变量通过指针定义的局部变量之前:

    int noName;
    {
        int * aliasToNoName = &noName; /* reference to global */
        int noName;                    /* declaration of local */
        *aliasToNoName = noName;       /* assign local to global */
    }
    
answer5: 回答5:

what is a "shielded global variable" in pure C?

in C you have local variables, file local/global variables (static) and global variables (extern)

so file1.c:
int bla;

file2.c
extern int bla;

纯C中的“屏蔽全局变量”是什么?

C你有局部变量,局部/全局变量文件(静态)和全局变量(外部)

so file1.c:
int bla;

file2.c
extern int bla;
answer6: 回答6:

Yet another option is to reference the global before defining your local, or at least get a pointer to it first so you can access it after defining your local.

#include <stdio.h>

int x = 1234;
int main()
{
   printf("%d\n",x); // prints global
   int x = 456;
   printf("%d\n",x); // prints local
}

另一种选择是在定义你的本地之前引用全局,或者至少先得到一个指针,这样你就可以在定义你的本地之后访问它。

#include <stdio.h>

int x = 1234;
int main()
{
   printf("%d\n",x); // prints global
   int x = 456;
   printf("%d\n",x); // prints local
}
answer7: 回答7:

gyz y 2 think so much Just use pointer variable n store the address of the global variable in it n den use it inside d main(). using the pointer to refer the global variable will not be a problem even if a local variable with same name is there inside main().

gyz y 2 think so much Just use pointer variable n store the address of the global variable in it n den use it inside d main(). using the pointer to refer the global variable will not be a problem even if a local variable with same name is there inside main().

c  scope  global-variables