简体   繁体   English

有没有办法访问 main.c 中定义的静态全局变量并在另一个文件中修改它?

[英]Is there any way to access static global variable defined in main.c and modify it in another file?

For example:例如:

In main.cmain.c

static glob_var;

I want to modify the value of glob_var in another file say file1.c我想在另一个文件中修改 glob_var 的值,比如file1.c

Making an variable static makes its identifier inaccessible from other translation unit (what usually means other C-file).将变量static会使其他翻译单元(通常意味着其他 C 文件)无法访问其标识符。 You can either你可以

  1. Make the variable non-static.使变量非静态。
//main.c

int glob_var;


//file1.c

extern int glob_var;

Note that the declaration should be put to a header file.请注意,声明应放在头文件中。

  1. Keep it static and add a helper function for access.保持静态并添加辅助函数以进行访问。
//main.c

static int glob_var;
void SetGlobVar(int val) {
  glob_var = val;
}


//file1.c

void SetGlobVar(int);
void foo(void) {
  SetGlobVar(42);
}

Note that the declaration of SetGlobVar() should be put to a header file.请注意, SetGlobVar()的声明应放在头文件中。

The comments and other answer address the question of how to access and modify a static global" . The following is offed as an alternative to using static for this purpose...评论和其他答案解决了如何访问和修改静态全局“的问题。以下是为此目的使用静态的替代方法......

When needing to create a variable that is global and can be changed among several translation units I believe it is more idiomatic to use the extern storage class .当需要创建一个全局变量并且可以在多个翻译单元之间进行更改时,我相信使用extern存储类更为惯用。 This is typically done by:这通常通过以下方式完成:

  • Declaring the extern variable in a header file在头文件中声明外部变量
  • Defining the extern variable one-time-only in a .c file that #includes s the header file.#includes头文件的 .c 文件中一次性定义extern 变量。
  • Access and modify the extern class variable from any translation unit that #include s the header file in which the extern is declared.从任何翻译单元访问和修改extern 类变量#include是声明extern的头文件。

Example:例子:

in some.h :some.h中:

void modify_glob_var(int val);
...
extern int glob_var;//declare extern variable

in main.cmain.c

#include "some.h"
...
int glob_var = 0;// define extern variable
...
modify_glob_var(10);//access and modify extern variable

in some_other.csome_other.c

include "some.h"
...
void modify_glob_var(int val)
{
    glob_var = val;//value of extern glob_var is changed here
}
     

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

相关问题 如何在另一个.c文件中定义的main.ca结构中使用? - How to use in main.c a structure defined in another .c file? 来自 main.c 的全局变量未在 header 文件 function 中声明 - global variable from main.c is undeclared in header file function main.c中定义的宏在另一个包含的文件中不可见 - Macro defined in main.c not visible in another included file C语言:使用枚举变量和在另一个文件内的main.c文件中定义的类型时遇到麻烦 - C Language: trouble using enumerated variable and type definited in main.c file inside another file c:检查是否从外部文件在main.c中定义了函数 - c: check if a function is defined in main.c from external file 从C中的另一个文件访问全局静态变量 - Access a global static variable from another file in C 如何将变量从main.c转换为另一个文件(中断处理程序) - How can I transform a Variable from main.c to another file ( interrupt handler) 将.c文件与main.c文件链接 - Link a .c file with a main.c file main.c无法读取我在.h文件中声明并在另一个.c文件(在C中)定义的函数 - main.c cannot read functions that i declare in .h file and define in another .c file (in C) 如何将 main.c 链接到 Xcode 项目中不同目录的另一个 C 文件 - How to link main.c to another C file from different directory in Xcode Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM