



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Array elements are accessed using the Subscript variable, Similarly Structure members are ... Nested Structures are allowed in C Programming Language.
Typology: Summaries
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Example : #include<stdio.h> #include<conio.h>
struct Vehicle { int wheels; char vname[20]; char color[10]; } v1 = {4,"Maruti 800","White"};
void main () { printf ("Vehicle No of Wheels : %d",v1.wheels); printf("Vehicle Name : %s",v1.vname); printf("Vehicle Color : %s",v1.color); getch(); }
Structure written inside another structure is called as nesting of two structures.
Nested Structures are allowed in C Programming Language.
We can write one Structure inside another structure as member of another structure.
struct date { int date; int month; int year; };
struct Employee { char ename[20]; int ssn; float salary; struct date doj; } emp1;
Accessing Month Field: emp1.doj.month
Accessing day Field : emp1.doj.day
Accessing year Field: emp1.doj.year
struct Employee { char ename[20]; int ssn; float salary; struct date { int date; int month; int year; }doj; }emp1;
Accessing Nested Members :
Accessing Month Field : emp1.doj.month
Accessing day Field : emp1.doj.day
Accessing year Field : emp1.doj.year
t1 = {"India",11,"Dhoni"} , *sptr = &t1;
int main() {
printf("\nTeam : %s",(sptr).name); printf("\nMemebers : %d",sptr->members); printf("\nCaptain : %s",(sptr).captain);
return 0; }
Passing Structure to Function in C Programming
Example:
#include<stdio.h> #include<conio.h> //------------------------------------- struct Example { int num1; int num2; }s[3]; //------------------------------------- void accept( struct Example *sptr) { printf("\nEnter num1 : "); scanf("%d",&sptr->num1); printf("\nEnter num2 : "); scanf("%d",&sptr->num2); } //------------------------------------- void print( struct Example *sptr) { printf("\nNum1 : %d",sptr->num1); printf("\nNum2 : %d",sptr->num2); } //------------------------------------- void main() { int i;
clrscr(); for (i=0;i<3;i++) accept(&s[i]);
for (i=0;i<3;i++) print(&s[i]);
getch(); }
Accessing Element in Structure Array
Example:
#include<stdio.h> #include<conio.h>
struct Employee { int ssn; char ename[20]; char dept[20]; }emp[3];
//--------------------------------------------- void main() { int i,sum;
//Enter the Employee Details for (i=0;i<3;i++) { printf("nEnter the Employee Details : "); scanf("%d %s %s",&emp[i].ssn,emp[i].ename,emp[i].dept); } //Print Employee Details for (i=0;i<3;i++) { printf("nEmployee SSN : %d",emp[i].ssn);