在一个结构中包含另一个结构,即嵌套关系。
关于嵌套结构的声明:
struct names handle;
该声明表明handle是一个struct names 类型的变量,同时程序也应该包含结构names 的声明。
访问嵌套结构成员
fellow.handle.first
程序示例:
#include<stdio.h>
#define LEN 20
const char *msgs[50] =
{
" thank you for the wonderful evening,",
"you centertaily prove that a ",
"is a special kind of guy,we must get together",
"over a delicious ",
" and have s few laughs"
};
//第一个结构
struct names {
char first[LEN];
char last[LEN];
};
//第二个结构
struct guy {
struct names handle; //嵌套结构
char favfood[LEN];
char job[LEN];
float incone;
};
int main()
{
//初始化一个结构变量
struct guy fellow = {
{"Ewen","Villard"},
"grilled salmon",
"personality coach",
68112.00
};
printf("Dear %s,\n\n", fellow.handle.first);
printf("%s%s.\n", msgs[0], fellow.handle.first);
return 0;
}