Programming in C: Unit IV: Structures and Union

Arrays of Structures

with Example C Programs

In the above examples, we have seen how to declare a structure and assign values to its data members. Now we will discuss how to declare an array of a structure. For this purpose, let us first analyse, where we would need array of structures.

ARRAYS OF STRUCTURES

In the above examples, we have seen how to declare a structure and assign values to its data members. Now we will discuss how to declare an array of a structure. For this purpose, let us first analyse, where we would need array of structures.

Programming Tip: It is an error to omit array subscripts when referring to individual structures of an array of structures.

In a class, we do not have just one student. But there may be at least 30 students. So the same definition of the structure can be used for all the 30 students. This would be possible when we create an array of the structure. An array of a structure is declared in the same way as we declare an array of a built-in data type.

Another example where an array of structures is desir- able is in case of an organization. An organization has a number of employees. So, defining a separate structure for every employee is not a viable solution. So here we can have a common structure definition for all the employees. This can again be done by declaring an array of the struc- ture employee.

The general syntax for declaring an array of a structure can be given as

struct struct name

{

data_type member_name1;

data_type member_name2;

data_type member_name3;

…………………………………

};

struct struct_name struct_var [index];

Consider the given structure definition.

struct student

{

int r_ no;

char name [20];

char course [20];

float fees;

};

A student array can be declared simply by writing

student stud [30];

Now, to assign values to the ith student of the class, we will write,

stud[i].r_no = 09;

stud[i].name = "RASHI";

stud[i].course = "MCA";

stud[i].fees = 60000;

In order to initialize the array of structure variables at the time of declaration, you should write as follows:

student stud [3] = {{01, "Aman", "BCA", 45000}, {02, "Aryan", "MCA", 60000}, {03, "John", "BCA", 45000}};

6. Write a program to read and display the information of all the students in the class.

#include <stdio.h>

#include <conio.h>

int main()

{

struct student

{

int roll_no;

char name [80];

int fees;

char DOB [80];

};

struct student stud [50];

int n, i;

clrscr();

printf("\n Enter the number of students: ");

scanf("%d",&n);

for (i = 0; i < n;i++)

{

printf("\n Enter the roll number: ");

scanf("%d", &stud [i].roll_no);

printf("\n Enter the name: ");

gets (stud [i].name);

printf("\n Enter the fees: ");

scanf("%d", &stud [i].fees);

printf("\n Enter the DOB: ");

gets (stud [i].DOB);

}

for (i=0;i<n;i++)

{

printf("\n ********DETAILS OF STUDENT %d ******", i+1);

printf("\n ROLL No. = %d", stud[i]. roll_no);

printf("\n NAME = %%s", stud[i].name);

printf("\n FEES = %d", stud[i].fees);

printf("\n DOB = %s", stud [i].DOB);

}

getch();

return 0;

}

Output

Enter the number of students: 2

Enter the roll number: 1

Enter the name: kirti

Enter the fees: 5678

Enter the DOB: 9 9 91

Enter the roll number: 2

Enter the name: kangana

Enter the fees: 5678

Enter the DOB: 27 8 91

********DETAILS OF STUDENT 1*******-

ROLL No. = 1

NAME = kirti

FEES = 5678

DOB = 9 9 91

********DETAILS OF STUDENT 2****

ROLL NO. = 2

NAME = kangana

FEES = 5678

DOB = 27 8 91

7. Write a program to read and display the information of all the students in the class. Then edit the details of the ith student and redisplay the entire information.

#include <stdio.h>

#include <string.h>

#include <conio.h>

int main()

struct student

{

int roll no;

char name [80];

int fees;

char DOB [80];

};

struct student stud [50];

int n, i, rolno, new_rolno;

int new_fees;

char new_DOB [80], new_name [80];

clrscr();

printf("\n Enter the number of students: ");

scanf("%d", &n);

for (i = 0;i<n;i++)

{

printf("\n Enter the roll number: ");

scanf("%d", &stud[i].roll_no);

printf("\n Enter the name: ");

gets (stud [i].name);

printf("\n Enter the fees: ");

scanf("%d", stud[i].fees);

printf("\n Enter the DOB: ");

gets (stud [i].DOB);

}

for (i = 0;i < n;i++)

{

printf("\n ******DETAILS OF STUDENT %d*******", i+1);

printf("\n ROLL No. = %d", stud[i]. = roll_no);

printf("\n NAME =%s", stud[i].name)

printf("\n FEES %d", stud[i].fees);

printf("\n DATE OF BIRTH = %s",stud [i].DOB);

}

printf("\n Enter the roll no. of the student whose record has to be edited: ");

scanf("%d", &rolno);

printf("\n Enter the new roll number: ");

scanf("%d", &new_rolno);

printf("\n Enter the new name:");

scanf("%s", new_name);

printf("\n Enter the new fees: ");

scanf("%d", &new_fees);

printf("\n Enter the new date of birth: ");

scanf("%s", new_DOB);

stud [rolno]. roll_no = new_rolno;

strcpy (stud [rolno]. name, new_name);

stud [rolno].fees = new_fees;

strcpy(stud [rolno]. DOB, new_DOB);

for (i=0;i<n;i++)

{

printf("\n ********DETAILS OF STUDENT %d*******", i+1);

printf("\n ROLL No.= %d", stud[i].roll_no);

printf("\n NAME = %s", stud[i].name);

printf("\n FEES = %d", stud[i].fees);

printf("\n DATE OF BIRTH = %s", stud[i].DOB);

}

getch();

return 0;

}

Output

Enter the number of students: 2

Enter the roll number: 1

Enter the name: kirti

Enter the fees: 5678

Enter the DOB: 9 9 91

Enter the roll number: 2

Enter the name: kangana

Enter the fees: 5678

Enter the DOB: 27 8 91

********DETAILS OF STUDENT 1*******

ROLL NO. = 1

NAME = kirti

FEES = 5678

DOB = 9 9 91

********DETAILS OF STUDENT 2***

ROLL NO. = 2

NAME = kangana

FEES = 5678

DOB = 27 8 91

Enter the roll no. of the student whose record has to be edited: 2

Enter the new roll number: 2

Enter the new name: kangana khullar

Enter the new fees: 7000

Enter the new date of birth: 27 8 92

********DETAILS OF STUDENT 1**********

ROLL No. = 1

NAME = kirti

FEES = 5678

DOB = 9 9 91

********DETAILS OF STUDENT 2*********

ROLL NO.= 2

NAME = kangana

FEES = 7000

DOB = 27 8 92

Programming in C: Unit IV: Structures and Union : Tag: : with Example C Programs - Arrays of Structures


Programming in C: Unit IV: Structures and Union



Under Subject


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation



Related Subjects


Professional English II

HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Statistics and Numerical Methods

MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation


Engineering Graphics

GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Physics for Electrical Engineering

PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Basic Civil and Mechanical Engineering

BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Electric Circuit Analysis

EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Physics for Electronics Engineering

PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Electrical and Instrumentation Engineering

BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Circuit Analysis

EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Materials Science

PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Physics for Civil Engineering

PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Basic Electrical, Electronics and Instrumentation Engineering

BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Physics for Information Science

PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation