0% found this document useful (0 votes)
50 views4 pages

Exp 4 PDF

The document discusses different ways of using structures in C programming: 1) Nested structures to store name and date of birth details of a person 2) Array of structures to store details of multiple students 3) Typedef to define an alias for a structure 4) Pointers to structures to store and print details of multiple people

Uploaded by

Corvo Attano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Exp 4 PDF

The document discusses different ways of using structures in C programming: 1) Nested structures to store name and date of birth details of a person 2) Array of structures to store details of multiple students 3) Typedef to define an alias for a structure 4) Pointers to structures to store and print details of multiple people

Uploaded by

Corvo Attano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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