Array of Structures tutorials

Accessing members

Members of structure variable are accessed by dot (.) operator. For example
s1.rollno, s1.name, s1.percert.

One cannot perform any operation on structure variable.

Operation such as input, output and other operations are carried out on
member variables.

Copying of one structure variable into another structure variable is
permitted. Here member to member coping takes place.

Structure variable can be sent to function and it can be returned from
function. In such cases argument type and return type of function is structure
type.

Member of a structure variable can be accessed by a pointer variable such
as p - >name.

In short Dot (.) operator is used for normal variable and ( -> )
operator is used with pointer variable.Dot(.) operator is used where the structure
is passed by value and the pointer (->)operator is used where the structure
is passed by address.

Sending a structure to a function

If a function has a structure as a parameter the entire structure has to be
sent as a parameter.

If only one member of the structure is required then only that variable is
sent to the function.

For functions having the return data types that of the structure.In that
case the structure variable must be copied to another structure variable of the
same type in calling function first and then members can be accessed.

Array of structures

Since structure is a data type an array of structures can also be created.

For eg. To maintain the records of 50 employees we just need to declare an
array of employee structures can be created.

Let us consider the following example

struct employee

{

char name[40];

float salary;

};

An array of employee can be declared as

employee e[5];

Each member of the employee array can be accessed as the same way we access
the array elements of a fundamental data type

employee e[5];

for (int i=0;i<5;i++)

{

printf(”Enter the name of the employee : ”);

scanf(“%s”,e[i].name);

printf(”Enter the salary of the employee : ”);

scanf(“%f”,e[i].salary);

}

Example: Program to write a structure which
accepts the student name,roll no and the marks of 3 subjects and then prints
the result based on the merit

/*WAP to write a structure which
accepts the student name,roll no and the marks of 3 subjects and then prints
the result based on the merit
*/

#include <stdio.h>

#include <conio.h>

void main()

{

struct student

{ char name[50];

int rno, marks[4];

} st[50];

int i, j, n = 3;

clrscr();

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

{

printf("Enter Name: ");

scanf("%s",&st[i].name);

printf("Enter Roll No: ");

scanf("%d",&st[i].rno);

st[i].marks[3] = 0;

for(j = 0; j < 3; j++)

{

printf("Enter Marks of Subject No %d :",j+1);

scanf("%d",&st[i].marks[j]);

st[i].marks[3] += st[i].marks[j];

}

}

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

for(j = i+1; j < n; j++)

if(st[j].marks[3] > st[i].marks[3])

{ student temp = st[i];

st[i] = st[j];

st[j] = temp;

}

printf("Names in the order of merit are\n");

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

printf("%s\n",st[i].name);

getch();

}

Output

Enter Name: Sheetal

Enter Roll No: 13

Enter Marks of Subject No 1 :9

Enter Marks of Subject No 2 :10

Enter Marks of Subject No 3 :10

Enter Name: Srushti

Enter Roll No: 9

Enter Marks of Subject No 1 :9

Enter Marks of Subject No 2 :9

Enter Marks of Subject No 3 :10

Enter Name: Sonali

Enter Roll No: 27

Enter Marks of Subject No 1 :8

Enter Marks of Subject No 2 :9

Enter Marks of Subject No 3 :10

Names in the order of merit are

Sheetal

Srushti

Sonali

Example: Program to write a structure which accepts the Name
and roll no of the student. The program should help the user to search
the name of the student by entering the roll no and the roll no by entering
the name of the student.

/*WAP to write a structure which accepts the Name and roll no of the
student.

The program should help the user to search the name of the student by
entering the roll no and the roll no by entering the name of the student

*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

struct student

{

int rno;

char name[20];

};

void read(student s[], int n)

{

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

{

printf("Enter name: ");

scanf("%s",&s[i].name);

printf("Enter roll no: ");

scanf("%d",&s[i].rno);

}

}

void main()

{

student s[10];

clrscr();

int n = 5;

read(s, n);

clrscr();

for(int i = 1; i<= n; i++)

{

clrscr();

printf("Searching Menu\n");

printf("Enter 1 to search name from roll no\nEnter 2 to
search roll no from name\n");

printf("Enter your choice: ");

int choice;

scanf("%d",&choice);

if(choice == 1)

{

printf("Enter Roll No: ");

int no;

scanf("%d",&no);

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

if(s[i].rno == no)

{

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

break;

}

if(i==n)

printf("No not found\n");

}

else if(choice == 2)

{

printf("Enter name: ");

char nm[20];

scanf("%s",&nm);

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

if(strcmp(s[i].name, nm) == 0)

{

printf("Roll No = %d\n",s[i].rno);

break;

}

if(i==n)

printf("Name not found\n");

}

else

printf("Wrong Choice\n");

printf("press a key to continue\n");

getch();

}

}

Output

Enter name: Kate

Enter roll no: 1

Enter name: James

Enter roll no: 2

Enter name: Dennis

Enter roll no: 3

Enter name: Mark

Enter roll no: 4

Enter name: Larry

Enter roll no: 5

Searching Menu

Enter 1 to search name from roll no

Enter 2 to search roll no from name

Enter your choice: 1

Enter Roll No: 1

Name = Kate

press a key to continue

Searching Menu

Enter 1 to search name from roll no

Enter 2 to search roll no from name

Enter your choice: 2

Enter name: Kate

Roll No = 1

press a key to continue

Searching Menu

Enter 1 to search name from roll no

Enter 2 to search roll no from name

Enter your choice: 2

Enter name: Larry

Roll No = 5

press a key to continue

Searching Menu

Enter 1 to search name from roll no

Enter 2 to search roll no from name

Enter your choice: 3

Wrong Choice

press a key to continue

Thank you for visiting www.cbtSAM.com. This site is dream of Samir Patel, when most people dream when in sleep, my dream doesn't let me sleep. cbtSAM Web site is provided as a service and learning tool to the public. While the information contained within the site is periodically updated,no guarantee is given that the iormation provided in this Web site is correct, complete, and up-to-date. We are not responsible for... Read More .