Professional Documents
Culture Documents
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:
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:
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:
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: