结构体的描述及嵌套
结构体
1.产生及意义
2.类型描述
struct 结构体名
{
数据类型 成员1;
数据类型 成员2;
...............
};
3.嵌套定义
struct birthday_st{
int year;
int month;
int day;
};
struct student_st{
int id;
char name[NAMESIZE];
struct birthday_st birth;
int math;
int Chinese;
};
struct student_st{
int id;
char name[NAMESIZE];
struct birthday_st{
int year;
int month;
int day;
}birth;
int math;
int Chinese;
};
4.定义变量(变量、数组、指针),初始化及成员引用
成员的引用:
变量名.成员名
指针->成员名
(*指针).成员名
成员的赋值:
struct simp_st a={123,456.789,'a'};
struct student_st stu = {191100153,"fanfan",{2000,12,9},100,100};
struct student_st stu ={.math = 97,.Chinese = 97};(部分成员赋值)
#include#include struct simp_st{ int i; float f; char ch; }; int main() { struct simp_st a={123,456.789,'a'}; a.i = 112233; printf("%i %f %cn",a.i,a.f,a.ch); exit(0); }
#include#include #define NAMESIZE 200 struct student_st{ int id; char name[NAMESIZE]; struct birthday_st{ int year; int month; int day; }birth; int math; int Chinese; }; int main() { struct student_st stu = {191100153,"fanfan",{2000,12,9},100,100}; printf("%i %s %i-%i-%i %i %in",stu.id,stu.name,stu.birth.year,stu.birth.month,stu.birth.day,stu.math,stu.Chinese); exit(0); }
执行结果如下:
#include#include #define NAMESIZE 200 struct student_st{ int id; char name[NAMESIZE]; struct birthday_st{ int year; int month; int day; }birth; int math; int Chinese; }; int main() { struct student_st arr[2] = {{10011,"kiki",{2000,10,1},99,66},{10012,"fanfan",{2000,12,9},99,99}}; struct student_st *p; p = arr; for(int i=0;i<2;i++,p++) //地址加一,一维数组数加一 { printf("%i %s %i-%i-%i %i %in",p->id,p->name,p->birth.year,p->birth.month,p->birth.day,p->math,p->Chinese); } exit(0); }
执行结果如下:
5.结构体占用空间的内存大小
struct simp_st a;
struct simp_st *p = &a;
printf("sizeof(a) = %dn",sizeof(a));
printf("sizeof(p) = %dn",sizeof(p));
让结构体不对齐的方法:
在结构体的·定义下面加一个宏:__attribute__((packed))
#include#include struct simp_st{ int i; float f; char ch; }__attribute__((packed)); int main() { struct simp_st a; struct simp_st *p = &a; printf("sizeof(a) = %dn",sizeof(a)); printf("sizeof(p) = %dn",sizeof(p)); exit(0); }
执行结果如下:
6.函数传参(值,地址)
值传递:func(struct simp_st *b)
地址传递:func(struct simp_st b)
微型学生管理系统
数组名就是数组的起始位置是一个常量,坚决不能放到等号左边,所以使用strcpy给p->name赋值
#include#include #include #define NAME_SIZE 32 struct student_st{ int id; char name[NAME_SIZE]; int math; int chinese; }; void stu_set(struct student_st *p,struct student_st *q) { *p = *q; } void stu_show(struct student_st *p) { printf("%d %s %d %dn",p->id,p->name,p->math,p->chinese); } void stu_changename(struct student_st *p,const char *newname) { strcpy(p->name,newname); } void menu(void) { printf("n1 setn2 change namen3 shown"); printf("Please enter the num:"); } int main() { struct student_st stu,tmp; char newname[NAME_SIZE]; int choice; int ret; do{ menu(); ret = scanf("%d",&choice); if(ret != 1) break; switch(choice) { case 1: printf("please enter for the stu[id name math chinese]:"); scanf("%d%s%d%d",&tmp.id,tmp.name,&tmp.math,&tmp.chinese); stu_set(&stu,&tmp); break; case 2: printf("please enter the newname:"); scanf("%s",newname); stu_changename(&stu,newname); break; case 3: stu_show(&stu); break; default: exit(1); } }while(1); exit(0); }
执行结果如下:
共用体
1. 产生及意义
2. 类型描述
union 共用体
{
数据类型 成员名1;
数据类型 成员名2;
...............
};
共用体只有一位成员是有效的
3. 嵌套定义
4. 定义变量(变量、数组、指针) 初始化及成员引用
成员引用:
变量名.成员名
指针名->成员名
5. 占用内存大小
6.函数传参(值、地址)
经常面试:7.位域
#include#include union { struct { char a:1; char b:2; char c:1; }x; int y; }w; int main() { w.y = 6; printf("%in",w.x.b); exit(0); }
#include#include #include union { struct { uint16_t i; uint16_t j; }x; uint32_t y; }a; int main() { a.y = 0x11223344; printf("%xn",a.x.i + a.x.j); exit(0); }
执行结果如下:
unsigned int i = 0x11223344;
printf("%xn",(i >> 16) + i&0xFFFF);
32位的高16位和低16位求和
枚举类型
enum
{
MON,
...,
SUN
};



