简体   繁体   English

从C中的另一个文件访问全局静态变量

[英]Access a global static variable from another file in C

In C language, I want to access a global static variable outside the scope of the file. 在C语言中,我想访问文件范围之外的全局静态变量。 Let me know the best possible way to do it. 让我知道最好的方法。 One of the methods is to assign an extern global variable the value of static variable, 其中一种方法是为外部全局变量赋值静态变量的值,

In file ac 在档案中

static int val = 10;
globalvar = val;

In file bc 在文件bc中

extern globalvar;

But in this case any changes in val(file ac) will not be updated in globalvar in (file bc). 但在这种情况下,val(文件ac)中的任何更改都不会在(文件bc)中的globalvar中更新。

Please let me know how can I achieve the same. 请让我知道如何实现同样的目标。

Thanks, Sikandar. 谢谢,Sikandar。

Well, if you can modify file ac then just make val non-static. 好吧,如果你可以修改文件ac那么只需使val非静态。

If you can modify ac but can't make val non-static (why?), then you can just declare a global pointer to it in ac 如果你可以修改ac但是不能使val非静态(为什么?),那么你可以在ac声明一个全局指针

int *pval = &val;

and in bc do bc

extern int *pval;

which will let you access the current value of val through *pval . 这将允许您通过*pval访问val的当前值。 Or you can introduce a non-static function that will return the current value of val . 或者您可以引入一个非静态函数,它将返回val的当前值。

But again, if you need to access it from other translation units, just make it non-static. 但同样,如果您需要从其他翻译单元访问它,只需将其设置为非静态。

You can make the global variable pointer to the global static variable. 您可以将全局变量指针指向全局静态变量。

/* file  a.c */
static int a = 100; /* global static variable not visible outside this file.*/
int *b = &a; /* global int pointer, pointing to global static*/


/* file b.c */
extern int *b; /* only declaration, b is defined in other file.*/

int main()
{
        printf("%d\n",*b); /* dereferencing b will give the value of variable a in file a.c */
        return 0;
}

On running: 在跑步时:

$ gcc *.c && ./a.out
100

You cannot access a file level static variable outside of the file. 您无法访问文件外部的文件级静态变量。

If you truly need to do that, you have a couple of choices. 如果你真的需要这样做,你有几个选择。

  1. Add an accessor function to the file that has the static variable. 将访问器函数添加到具有静态变量的文件中。 The nice thing is this restricts access from outside the file to read-only access: 好处是这限制了从文件外部访问只读访问:

    int read_static() { return val; int read_static(){return val; } }

  2. Drop the static qualifier and make the variable a global. 删除静态限定符并使变量成为全局变量。

Solution 1: 解决方案1:

In file ac 在档案中

static int val=10;
int *globalvar =&val;

In file bc 在文件bc中

extern int *globalvar;

Solution 2: 解决方案2:

Instead of having another variable to pass the address of the static variable thereby adding few memory bytes wastage, make the static variable itself as extern. 而不是让另一个变量传递静态变量的地址,从而增加几个内存字节的浪费,使静态变量本身作为外部。

Solution 2 is recommended, but in case if you are restricted to changing the static variable to extern, use solution 1. 建议使用解决方案2,但是如果您仅限于将静态变量更改为extern,请使用解决方案1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM