can define the data structure in one header, initialize in another one
can include header files like this or include both headers in source file without including test_define.h in test_ini.h. but extern instance in source file in required since you have defined it during initialization
// test_define.h
#include <stdint.h>
typedef struct a_s
{
uint8_t num1;
uint16_t num2;
}a_t;// test_init.h
#include "test_define.h"
a_t a1 = {
.num1 = 2,
.num2 = 3
}; // test.c
#include <stdio.h>
#include "test_init.h"
extern a_t a1;
int main() {
printf("a1.num1 = %d, num2 = %d\n", a1.num1, a1.num2);
return 0;
} // main end
本文探讨了如何在一个头文件中定义数据结构,并在另一个头文件中进行初始化。通过使用预处理器指令,实现高效的数据组织与操作。示例代码展示了如何在源文件中声明并实例化已定义的数据结构。
820

被折叠的 条评论
为什么被折叠?



