目录
一.结构体嵌套
结构体嵌套指的是在结构体中含有结构体类型的成员。
举例
定义一个结构体用来存放一个人姓名,性别,出生日期,年龄,就可以使用结构体嵌套。
struct base {
int y;
int m;
int d;
int age;
};
struct person {
char name[20];
char sex;
struct base myage;
};
二.定义与初始化
/*
struct person p1 = {
"wang",
1,
{2020,01,01,24},
};
*/
struct person p1 = {
.sex = 1,
.myage.age = 24,
};
三.结构体嵌套成员访问
printf("age is %d\n",p1.myage.age);
四.综合例子
#include <stdio.h>
struct other
{
float price;
unsigned int date;
};
struct book
{
char title[128];
char author[128];
struct other boot_other;
};
int main(void)
{
//struct book mybook;
/*
struct book mybook = {
"mybook",
"me",
20.9,
20200101,
};
*/
struct book mybook = {
.title = "mybook",
.boot_other.price = 29,
};
mybook.boot_other.price = 22.5;
printf("mybook.price is %.1f\n",mybook.boot_other.price );
printf("mybook.title is %s\n",mybook.title);
return 0;
}