c- structure initialization

来源:互联网 发布:剑灵可爱召唤捏脸数据 编辑:程序博客网 时间:2024/06/05 11:52

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


0 0
原创粉丝点击