多个文件共享结构体变量

来源:互联网 发布:虚拟机安装mac卡 编辑:程序博客网 时间:2024/06/13 08:20

一、c语言如何调用其他文件定义的结构体成员
1、假设有a、b两个文件

    做法一:在a.c中定义结构体            struct student strStudent[10];                在a.h中写明结构体的具体成员            struct student {                    char *name;                    int age;                    int sex;            };                b文件中引用a文件中结构体                #include “a.h”                extern struct student strStudent[10]; //或者将此句搬移到a.h中也行                void main(){                strStudent[0].name = "TOM";                return 0;                }    做法二:在a.c中定义结构体             student_t stu;                在a.h中写明结构体的具体成员                typedef struct student {                    char *name;                    int age;                    int sex;            }student_t;                b文件中引用a文件中结构体                #include “a.h”                extern student_t stu; //或者将此句搬移到a.h中也行                void main(){                stu.name = "TOM";                return 0;                }
原创粉丝点击