您好,欢迎访问一九零五行业门户网

C语言中的嵌套结构是什么?

结构内的结构(或)嵌套结构
另一个结构内的结构称为嵌套结构。
考虑以下示例,
>struct emp{ int eno; char ename[30]; float sal; float da; float hra; float ea;}e;
所有属于配额的项目都可以组合在一起并在子结构下申报,如下所示。
stuct emp{ int eno; char ename[30]; float sal; struct allowance{ float da; float hra; float ea; }a;}e;
嵌套结构中最里面的成员可以通过使用点运算符更改所有相关结构变量(从最外层到最内层)来访问该成员。
程序以下程序是演示嵌套结构(结构内的结构) -
 live demo
#include<stdio.h>//declaring outer and inter structures//struct person//main structure//{ char name[500]; int age; char gender; char temp;//to clear buffer// struct address//nested structure//{ char apartment[500]; char street[500]; char city[100]; char state[100]; int zipcode; }a[20];//nested structure variable// }p[20];//main structure variable//void main(){ //declaring variable for for loop// int i; //reading user i/p// for (i=1;i<3;i++){//declaring function to accept 2 people's data// printf("enter the name of person %d : ",i); gets(p[i].name); printf("enter the age of person %d : ",i); scanf("%d",&p[i].age); scanf("%c",&p[i].temp);//clearing buffer// printf("enter the gender of person %d : ",i); scanf("%c",&p[i].gender); scanf("%c",&p[i].temp);//clearing buffer// printf("enter the city of person %d : ",i); gets(p[i].a[i].city); printf("enter the state of person %d : ",i); gets(p[i].a[i].state); printf("enter the zip code of person %d : ",i); scanf("%d",&p[i].a[i].zipcode); scanf("%c",&p[i].temp);//clearing buffer// } //printing o/p// for (i=1;i<3;i++){ printf("the name of person %d is : %s
",i,p[i].name); printf("the age of person %d is : %d
",i,p[i].age); printf("the gender of person %d is : %c
",i,p[i].gender); printf("the city of person %d is : %s
",i,p[i].a[i].city); printf("the state of person %d is : %s
",i,p[i].a[i].state); printf("the zip code of person %d is : %d
",i,p[i].a[i].zipcode); }}
输出enter the name of person 1 : enter the age of person 1 : enter the gender of person 1 : enter the city of person 1 : enter the state of person 1 : enter the zip code of person 1 : enter the name of person 2 : enter the age of person 2 : enter the gender of person 2 : enter the city of person 2 : enter the state of person 2 : enter the zip code of person 2 : the name of person 1 is :the age of person 1 is : 0the gender of person 1 is :the city of person 1 is :the state of person 1 is :the zip code of person 1 is : 0the name of person 2 is :the age of person 2 is : 0the gender of person 2 is :the city of person 2 is :the state of person 2 is :the zip code of person 2 is : 0
以上就是c语言中的嵌套结构是什么?的详细内容。
其它类似信息

推荐信息