You are on page 1of 4

1.

Nested Structures

#include<stdio.h>
struct bod
{
int dt, year;
char month[10];
};
struct
{
char name[10];
struct bod date;
}person;

void main()
{
printf("Enter the name and date of birth of the person\n");
scanf("%s",person.name);
scanf("%d %s %d",&person.date.dt,person.date.month,&person.date.year);
printf("name: %s\n",person.name);
printf("BOD=%d-%s-%d\n",person.date.dt,person.date.month,person.date.year);
}

Output:

Enter the name and date of birth of the person


Sharmad
13
November
1999
name: Sharmad
BOD=13-November-1999

2. Array of Structures

#include<stdio.h>
struct students
{
char name[10];
int age;
char branch[10];
};
int main()
{
int n,i;
printf("Enter the number of students\n");
scanf("%d",&n);
struct students s[n];
printf("Enter the following details: Name, age, and branch\n");
for(i=0;i<n;i++)
{
scanf("%s %d %s",s[i].name,&s[i].age,s[i].branch);
}
printf("The details are: \n");
for(i=0;i<n;i++)
{
printf("%s %d %s\n",s[i].name,s[i].age,s[i].branch);
}
}

Output:

Enter the number of students


3
Enter the following details: Name, age, and branch
Yuu
19
CSE
Ren
18
ECE
Shou
17
Mech
The details are:
Yuu 19 CSE
Ren 18 ECE
Shou 17 Mech

3. Typedef

#include<stdio.h>
typedef struct
{
int dt, year;
char month[10];
}bd;
struct
{
char name[10];
bd date;
}person;
void main()
{
printf("Enter the name and date of birth of the person\n");
scanf("%s",person.name);
scanf("%d %s %d",&person.date.dt,person.date.month,&person.date.year);
printf("name: %s\n",person.name);
printf("BOD=%d-%s-%d\n",person.date.dt,person.date.month,person.date.year);
}

Output:

Enter the name and date of birth of the person


Eric
11
August
2018
name: Eric
BOD=11-August-2018

4. Pointers to Structures:

#include<stdio.h>
struct bod
{
int dt, year;
char month[10];
};
struct person
{
char name[10];
struct bod date;
};
struct person p[20];

void main()
{
struct person *n[20];
struct person s;
int i,m;
printf("Enter the number of people\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the data of person %d (Name and BOD)\n",i+1);
scanf("%s",p[i].name);
scanf("%d %s %d", &p[i].date.dt,p[i].date.month,&p[i].date.year);

}
for(i=0;i<m;i++)
{
printf("The details of person %d are:\n",i+1);
n[i]=&p[i];
s=*n[i];
printf("%s %d %s %d\n",s.name,s.date.dt,s.date.month,s.date.year);
}
}

Output:

Enter the number of people


2
Enter the data of person 1 (Name and BOD)
sa
22
feb
2010
Enter the data of person 2 (Name and BOD)
dgh
2
aug
2007
The details of person 1 are:
sa 22 feb 2010
The details of person 2 are:
dgh 2 aug 2007

You might also like