假设我有一个结构
struct A
{
int x;
int y;
}s;
现在假设我想在多个 c 文件中共享这个结构变量,即“s”。我怎样才能实现这个目标。
我应该使用 extern 还是必须使用头文件并将该头文件包含在每个 c 文件中
最佳答案
分为头文件和代码文件。
然后在每个需要访问该变量的代码文件中包含 header 。
(顺便说一句,全局变量应该小心使用。)
标题:
struct A
{
int x;
int y;
};
extern A s;
单个代码文件:
#include "theheader.h"
A s;
其他代码文件:
#include "theheader.h"
/* access the variable */
关于c - 如何在多个c文件中共享一个结构体变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44818491/