#include#include #define elemtype1 int #define elemtype2 char typedef struct student{ elemtype1 id; elemtype2 name[11]; elemtype2 sex; }stu; //结构体别名 typedef struct teacher{ elemtype1 *id; elemtype2 *name[11]; elemtype2 *sex; }teacher; //结构体别名 typedef struct brother{ elemtype1 id; elemtype2 name[11]; elemtype2 sex; }brother; //结构体别名 void PrintStu(stu *student) { printf("stu1 id is :%dn",student->id); printf("stu1 name is :%sn",student->name); printf("stu1 sex is :%cn",student->sex); } void PrintStu1(teacher *student) { printf("stu1 id is :%dn",student->id); printf("stu1 name is :%sn",student->name); printf("stu1 sex is :%cn",student->sex); } void PrintStu2(brother *student) { printf("stu1 id is :%dn",student->id); printf("stu1 name is :%sn",student->name); printf("stu1 sex is :%cn",student->sex); } void printf3n() { printf("n"); printf("n"); printf("n"); } elemtype1 main() { elemtype1 i; elemtype1 b=2022; elemtype2 c='m'; elemtype2 a[11]="chenguanye"; stu stu1; teacher teacher1; brother brother1; printf("*****************开始********************n"); printf3n(); teacher1.id=2022; strcpy(teacher1.name, "chenguanye"); teacher1.sex='m'; PrintStu1(&teacher1); printf("sizeof of teacher1 is :%dn",sizeof(teacher1)); printf("sizeof of teacher1.id is :%dn",sizeof(teacher1.id)); printf("sizeof of teacher1.name is:%dn",sizeof(teacher1.name)); printf("sizeof of teacher1.sex is :%dn",sizeof(teacher1.sex)); teacher *ss = (teacher*)malloc(sizeof(teacher)); //申请堆内存 ss->id=2022; strcpy(ss->name, "chenguanye"); ss->sex='m'; printf3n(); PrintStu1(ss); printf("sizeof of ss is :%dn",sizeof(ss)); printf("sizeof of ss->id is :%dn",sizeof(ss->id)); printf("sizeof of ss->name is:%dn",sizeof(ss->name)); printf("sizeof of ss->sex is :%dn",sizeof(ss->sex)); stu1.id=2022; strcpy(stu1.name, "chenguanye"); stu1.sex='m'; printf3n(); PrintStu(&stu1); printf("sizeof of stu1 is :%dn",sizeof(stu1)); printf("sizeof of stu1.id is :%dn",sizeof(stu1.id)); printf("sizeof of stu1.name is:%dn",sizeof(stu1.name)); printf("sizeof of stu1.sex is :%dn",sizeof(stu1.sex)); //结构体指针 stu *s = (stu*)malloc(sizeof(stu)); //申请堆内存 s->id = 2022; strcpy(s->name, "chenguanye"); s->sex = 'm'; printf3n(); PrintStu(s); printf("sizeof of s is :%dn",sizeof(s)); printf("sizeof of s->id is :%dn",sizeof(s->id)); printf("sizeof of s->name is:%dn",sizeof(s->name)); printf("sizeof of s->sex is :%dn",sizeof(s->sex)); brother1.id=b; for(i=0;i<10;i++) { brother1.name[i]=a[i]; } brother1.sex=c; printf3n(); PrintStu(&brother1); printf("sizeof of brother1 is :%dn",sizeof(brother1)); printf("sizeof of brother1.id is :%dn",sizeof(brother1.id)); printf("sizeof of brother1.name is:%dn",sizeof(brother1.name)); printf("sizeof of brother1.sex is :%dn",sizeof(brother1.sex)); //结构体指针 stu *sss = (brother*)malloc(sizeof(brother)); //申请堆内存 sss->id = b; for(i=0;i<10;i++) { sss->name[i]=a[i]; } sss->sex = c; printf3n(); PrintStu(sss); printf("sizeof of sss is :%dn",sizeof(sss)); printf("sizeof of sss->id is :%dn",sizeof(sss->id)); printf("sizeof of sss->name is:%dn",sizeof(sss->name)); printf("sizeof of sss->sex is :%dn",sizeof(sss->sex)); printf3n(); printf("*****************结束********************n"); }



