从下再访问文件中的全局静态变量 [英] Access a global static variable from another file in C

查看:181
本文介绍了从下再访问文件中的全局静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我要访问的文件范围以外的一个全局静态变量。让我知道,最好的方式做到这一点。
其中一种方法是分配一个外部的全局变量的静态变量的值,

In C language, I want to access a global static variable outside the scope of the file. 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,

在文件交流转换器

static int val = 10;
globalvar = val;

在文件b.c

extern globalvar;

但是,在这种情况下,VAL(文件交流转换器)的任何更改将不会在(文件b.c)globalvar更新。

请让我知道我怎么能达到同样的。

Please let me know how can I achieve the same.

谢谢,
Sikandar。

Thanks, Sikandar.

推荐答案

好吧,如果你可以修改文件 AC 则只是让 VAL 非静态的。

Well, if you can modify file a.c then just make val non-static.

如果您可以修改 AC ,但不能让 VAL 非静态(为什么?),那么你可以只声明一个全局指针指向它在 AC

If you can modify a.c but can't make val non-static (why?), then you can just declare a global pointer to it in a.c

int *pval = &val;

b.c 不要

extern int *pval;

这将让你通过访问 VAL 的当前值* PVAL 。或者你也可以引入,将返回的当前值的非静态函数 VAL

which will let you access the current value of val through *pval. Or you can introduce a non-static function that will return the current value of val.

但同样,如果你需要从其他翻译单元访问它,只是使它非静态的。

But again, if you need to access it from other translation units, just make it non-static.

这篇关于从下再访问文件中的全局静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆