C Programming – CSIT 1st Semester
Unit 8: Structure and Union
Introduction:
Array can be used to represent a group of data item that belong to the same type
such as int, float, etc. So, if we want to represent a collection of data item of
different types using a single name, we cannot use array.
So, in order to represent a collection of data item of different types using a single
name we use a constructed data type known as structure.
Structure is a conventional tool for handling a group of logically related data
items. Or Structure is a group of variables of different data types represented by a
single name.
For example: If I have to write a program to store Student information, which will
have Student's name, age, branch, permanent address, father's name etc, which
included string values, integer values etc, in such a situation we use structure.
Defining the structure
Before you can create structure variables, you need to define its data type. To
define a struct, the struct keyword is used. The general format of a structure is as
follows,
Syntax:
struct structure_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};
By Lec. Pratik Chand Page 1
C Programming – CSIT 1st Semester
The keyword struct declares a structure to hold the details of data fields. After
the struct keyword, there is structure name which is optional. The name of the
structure is called as the structure tag. Then inside the curly braces, we have to
mention all the member variables, which are normal C language variables of
different types like int, float, array etc.
Example
struct Student
{
char name[25];
int age;
char branch[10];
char gender;
};
Here struct Student declares a structure to hold the details of a student which
consists of 4 data fields, namely name, age, branch and gender. These fields are
called structure elements or members.
Student is the name of the structure and is called as the structure tag. In student
structure each member can have different datatype, like in this case, name is an
array of char type and age is of int type etc.
Declaring Structure Variables
It is possible to declare variables of a structure, either along with structure
definition or after the structure is defined. Structure variable declaration is similar
to the declaration of any normal variable of any other datatype. Structure variables
can be declared in following two ways:
1) Declaring Structure variables separately
struct Student
{
char name[25];
By Lec. Pratik Chand Page 2
C Programming – CSIT 1st Semester
int age;
char branch[10];
char gender;
};
struct Student S1, S2; //declaring variables of struct Student
2) Declaring Structure variables with structure definition
struct Student
{
char name[25];
int age;
char branch[10];
char gender;
}S1, S2;
Here S1 and S2 are variables of structure Student. However, this approach is
not much recommended.
Initialize a structure variable
C language supports multiple ways to initialize a structure variable. You can use
any of the initialization method to initialize your structure as follows.
Initialize using dot operator
Value initialized structure variable
Variant of value initialized structure variable
Let us declare a student structure containing three fields i.e. name, roll and marks.
struct student
{
char name[100];
int roll;
By Lec. Pratik Chand Page 3
C Programming – CSIT 1st Semester
float marks;
};
Initialize structure using dot operator
In C, we initialize or access a structure variable either through dot (.) or
arrow (->) operator. This is the easiest way to initialize or access a structure.
Example:
Now declare structure variable
struct student stu1;
// Initialize structure members
stu1.name = "Pratik";
stu1.roll = 1;
stu1.marks = 79.5;
Example:
#include<stdio.h>
#include<string.h>
struct Student
{
//string name;
char name[25];
int age;
char branch[10];
};
int main()
{
struct Student s1;
strcpy(s1.name,"Pratik");
By Lec. Pratik Chand Page 4
C Programming – CSIT 1st Semester
s1.age = 60;
strcpy(s1.branch,"BCA");
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
printf("Branch of Student 1: %s\n", s1.branch);
return 0;
}
Output:
Name of Student 1: Pratik
Age of Student 1: 60
Branch of Student 1: BCA
Value initialized structure variable
The above method is easy and straightforward to initialize a structure variable.
However, C language also supports value initialization for structure variable.
Means, you can initialize a structure to some default value during its variable
declaration.
Example:
// Declare and initialize structure variable
struct student stu1 = { "Pratik", 1, 79.5};
Note: The values for the value initialized structure should match the order in which
structure members are declared.
Example:
#include<stdio.h>
#include<string.h>
struct Student
{
//string name;
By Lec. Pratik Chand Page 5
C Programming – CSIT 1st Semester
char name[25];
int age;
char branch[10];
};
int main()
{
struct Student s1 = {"Rahul",25,"BBA"};
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
printf("Branch of Student 1: %s\n", s1.branch);
return 0;
}
Output:
Name of Student 1: Rahul
Age of Student 1: 25
Branch of Student 1: BBA
Variant of value initialized structure variable
In addition, C language supports flexibility to initialize structure members in any
order. This approach is an extension of above. Here, you can specify member name
along with the value.
Example:
// Declare and initialize structure variable
struct student stu1 = {
.roll = 1,
.name = "pratik",
.marks = 79.5
};
By Lec. Pratik Chand Page 6
C Programming – CSIT 1st Semester
Example:
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
};
int main()
{
struct Student s1 = {.age = 20, .name= "Hari", .branch = "BCA"};
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
printf("Branch of Student 1: %s\n", s1.branch);
return 0;
}
Output:
Name of Student 1: Hari
Age of Student 1: 20
Branch of Student 1: BCA
Accessing Structure Members
The member of structure can be accessed with the help of dot (.) operator as
follows:
s1.age;
Here s1 is the structure variable and age is the structure member. Hence s1.age is
the variable representing the age of s1 and can be treated like any other ordinary
variable.
By Lec. Pratik Chand Page 7
C Programming – CSIT 1st Semester
Nested structure:
A structure can be nested inside another structure. Hence, when a structure
contains another structure, it is called nested structure. Here is the syntax to create
nested structures.
Syntax:
structure tagname_1
{
member1;
member2;
member3;
...
member_n;
structure tagname_2
{
member_1;
member_2;
member_3;
...
member_n;
} var1;
} var2;
Example:
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
float Salary;
By Lec. Pratik Chand Page 8
C Programming – CSIT 1st Semester
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
}Addr;
}Emp;
void main()
{
printf("Enter Employee Id : ");
scanf("%d",&Emp.Id);
printf("Enter Employee Name : ");
scanf("%s",&Emp.Name);
printf("Enter Employee Salary : ");
scanf("%f",&Emp.Salary);
printf("Enter Employee House No : ");
scanf("%s",&Emp.Addr.HouseNo);
printf("Enter Employee City : ");
scanf("%s",&Emp.Addr.City);
printf("Enter Employee PinCode : ");
scanf("%s",&Emp.Addr.PinCode);
printf("\nDetails of Employees");
printf("\nEmployee Id : %d",Emp.Id);
printf("\nEmployee Name : %s",Emp.Name);
printf("\nEmployee Salary : %.2f",Emp.Salary);
printf("\nEmployee House No : %s",Emp.Addr.HouseNo);
By Lec. Pratik Chand Page 9
C Programming – CSIT 1st Semester
printf("\nEmployee City : %s",Emp.Addr.City);
printf("\nEmployee PinCode : %s",Emp.Addr.PinCode);
}
Output:
Enter Employee Id : 1
Enter Employee Name : Rahul
Enter Employee Salary : 40000
Enter Employee House No : 12
Enter Employee City : Kathmandu
Enter Employee PinCode : 4001
Details of Employees
Employee Id : 1
Employee Name : Rahul
Employee Salary : 40000.00
Employee House No : 12
Employee City : Kathmandu
Employee PinCode : 4001
Array of structure
As you know, C Structure is collection of different datatypes (variables) which are
grouped together. Whereas, array of structures is nothing but collection of
structures. This is also called as structure array in C.
Example:
#include<stdio.h>
struct student
{
char name[20];
char address[20];
By Lec. Pratik Chand Page 10
C Programming – CSIT 1st Semester
int roll;
};
int main()
{
int i,n;
printf("Enter the number of student:");
scanf("%d",&n);
struct student st[n]; // array of structure variable
for(i=0;i<n;i++)
{
printf("Enter Information for Student %d\n",i+1);
printf("Enter Name:");
scanf("%s",&st[i].name);
printf("Enter Address:");
scanf("%s",&st[i].address);
printf("Enter Roll No:");
scanf("%d",&st[i].roll);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("Information of Student %d\n",i+1);
printf("Name: %s \n",st[i].name);
printf("Address: %s \n",st[i].address);
printf("Roll No: %d \n",st[i].roll);
}
}
Output:
Enter the number of student:2
Enter Information for Student 1
Enter Name:Rahul
By Lec. Pratik Chand Page 11
C Programming – CSIT 1st Semester
Enter Address:ktm
Enter Roll No:1
Enter Information for Student 2
Enter Name:Sudan
Enter Address:Dang
Enter Roll No:2
Information of Student 1
Name: Rahul
Address: ktm
Roll No: 1
Information of Student 2
Name: Sudan
Address: Dang
Roll No: 2
Array within structure
As we know, structure is collection of different data type. Like normal data type, it
can also store an array as well. Hence, arrays may be the member within structure,
this is known as arrays within structure. Accessing arrays within structure is
similar to accessing other members.
Syntax:
struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN;
};
struct struct-name obj;
By Lec. Pratik Chand Page 12
C Programming – CSIT 1st Semester
Example:
#include<stdio.h>
struct Student
{
char Name[25];
int Marks[3];
int Total;
};
void main()
{
int i;
struct Student S;
printf("Enter Student Name : ");
scanf("%s",&S.Name);
S.Total = 0;
for(i=0;i<3;i++)
{
printf("\nEnter Marks of Subject %d : ",i+1);
scanf("%d",&S.Marks[i]);
S.Total = S.Total + S.Marks[i];
}
printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
}
Output:
Enter Student Name : Rahul
Enter Marks of Subject 1 : 80
Enter Marks of Subject 2 : 59
Enter Marks of Subject 3 : 7
Name : Rahul
Total : 217
By Lec. Pratik Chand Page 13
C Programming – CSIT 1st Semester
Passing Structure to function
A structure can be passed to any function from main function or from any sub
function. Structures are no different, when it comes to passing them to a function
or returning them from a function. We can pass a structure to a function in two
ways.
1. Pass by value.
2. Pass by reference.
Pass Structure to a Function by Value in C
In pass structure to function by value, the whole structure is passed to another
function by value. It means the whole structure is passed to another function with
all members and their values. So, this structure can be accessed from called
function.
If the structure is passed to the function by the value, then Changes made to the
structure variable members within the function will not reflect the original
structure members.
Example: Program to check whether the student is eligible for a scholarship or
not.
#include <stdio.h>
struct Student
{
char Name[50];
float First_Year_Marks;
float Second_Year_Marks;
};
int main()
{
struct Student Student1;
printf("Please Enter the Student Name: ");
By Lec. Pratik Chand Page 14
C Programming – CSIT 1st Semester
scanf("%s",&Student1.Name);
printf("Please Enter First Year Total Marks: ");
scanf("%f",&Student1.First_Year_Marks);
printf("Please Enter Second Year Total Marks: ");
scanf("%f",&Student1.Second_Year_Marks);
PassBy_Value(Student1);
return 0;
}
void PassBy_Value(struct Student std)
{
float Sum, Average;
Sum = std.First_Year_Marks + std. Second_Year_Marks;
Average = Sum/2;
if(Average > 460)
{
printf("%s is Eligible for Scholarship", std.Name);
}
else
{
printf("\n %s is Not Eligible for Scholorship",std.Name);
}
}
Output:
Please Enter the Student Name: Sahara
Please Enter First Year Total Marks: 460
Please Enter Second Year Total Marks: 490
By Lec. Pratik Chand Page 15
C Programming – CSIT 1st Semester
Sahara is Eligible for Scholarship
Passing Structure to a Function by Reference
In this case, the whole structure is passed to another function by address. It means
only the address of the structure is passed to another function. The whole structure
is not passed to another function with all members and their values. So, this
structure can be accessed from called function by its address.
Example:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student std;
std.id=1;
strcpy(std.name,"Roshan");
std.percentage = 86.5;
Record(&std);
return 0;
}
void Record(struct student *record)
{
printf("Student Id : %d \n", record->id);
printf("Name : %s \n", record->name);
By Lec. Pratik Chand Page 16
C Programming – CSIT 1st Semester
printf("Percentage : %.2f \n", record->percentage);
}
Output:
Student Id : 1
Name : Roshan
Percentage : 86.50
Passing Array of structure to function
An array structure can be passed to any function from main function or from any
sub function.
Example: program to pass an array of structures to a function in C
#include <stdio.h>
struct exam
{
char name[20];
int roll;
int marks;
};
int i,n;
int main()
{
printf("Please Enter the number of student: ");
scanf("%d",&n);
struct exam obj[n];
for (i=0;i<n;i++)
{
printf("Enter the detail of Student %d \n",i+1);
printf("Enter Name: ");
scanf("%s",&obj[i].name);
By Lec. Pratik Chand Page 17
C Programming – CSIT 1st Semester
printf("Enter Roll No : ");
scanf("%d",&obj[i].roll);
printf("Enter Total Marks: ");
scanf("%d",&obj[i].marks);
}
structfun(obj);
return 0;
}
void structfun(struct exam arr[])
{
for(i=0;i<n;i++)
{
printf("\nInformation of Student %d \n",i+1);
printf("Name : %s \n",arr[i].name);
printf("Roll No.: %d \n",arr[i].roll);
printf("Total Marks : %d\n",arr[i].marks);
printf("\n");
}
}
Output:
Please Enter the number of student: 2
Enter the detail of Student 1
Enter Name: Pukar
Enter Roll No : 1
Enter Total Marks: 480
Enter the detail of Student 2
Enter Name: Harish
Enter Roll No : 2
Enter Total Marks: 450
Information of Student 1
Name : Pukar
By Lec. Pratik Chand Page 18
C Programming – CSIT 1st Semester
Roll No.: 1
Total Marks : 480
Information of Student 2
Name : Harish
Roll No.: 2
Total Marks : 450
Pointer to Structure
Pointer is a variable which points to the address of another variable of any data
type like int, char, float etc. Similarly, we can have a pointer to structures, where a
pointer variable can point to the address of a structure variable. Here is how we can
declare a pointer to a structure variable.
Syntax:
struct name
{
member1;
member2;
.
.
};
int main()
{
struct name *ptr; // Pointer to structure variable
struct name Harry; // Structure variable
}
Here, ptr is a pointer to struct.
By Lec. Pratik Chand Page 19
C Programming – CSIT 1st Semester
Accessing member of structure using Pointer
There are two ways of accessing members of structure using pointer:
Using indirection (*) operator and dot (.) operator.
Using arrow (->) operator or membership operator.
Example 1: To access members of a structure using arrow (->) operator or
membership operator.
In this example, the address of person1 is stored in the personPtr pointer
using personPtr = &person1. Now, you can access the members of person1 using
the personPtr pointer.
#include <stdio.h>
// using membership operator ->
struct person
{
int age;
float weight;
};
int main()
{
struct person person1;
struct person *personPtr;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
By Lec. Pratik Chand Page 20
C Programming – CSIT 1st Semester
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %.2f", personPtr->weight);
return 0;
}
Output:
Enter age: 25
Enter weight: 78
Displaying:
Age: 25
weight: 78.00
Example 2: To access members of a structure using indirection (*) operator and
dot (.) operator.
#include <stdio.h>
// using indirection (*) and dot (.) operator
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d",&(*personPtr).age);
By Lec. Pratik Chand Page 21
C Programming – CSIT 1st Semester
printf("Enter weight: ");
scanf("%f",&(*personPtr).weight);
printf("Displaying:\n");
printf("Age: %d\n", (*personPtr).age);
printf("weight: %.2f", (*personPtr).weight);
return 0;
}
Output:
Enter age: 25
Enter weight: 80
Displaying:
Age: 25
weight: 80.00
Unions
A union is a special data type available in C that allows storing different data types
in the same memory location. Union provides an efficient way of reusing the
memory location, as only one of its members can be accessed at a time. A union is
used almost in the same way you would declare and use a structure.
Defining a Union:
To define a union, you must use the union statement in the same way as defining a
structure. The union statement defines a new data type with more than one member
for your program. A union is declared using the union keyword. The format of
the union statement is as follows:
By Lec. Pratik Chand Page 22
C Programming – CSIT 1st Semester
Syntax:
union union name
{
member definition;
member definition;
...
member definition;
};
Unions are conceptually similar to structures. The syntax to declare/define a union
is also similar to that of a structure. The only differences is in terms of storage.
In structure each member has its own storage location, whereas all members
of union use a single shared memory location which is equal to the size of its
largest data member.
This implies that although a union may contain many members of different
types, it cannot handle all the members at the same time. In union, all members
share the same memory location.
By Lec. Pratik Chand Page 23
C Programming – CSIT 1st Semester
Example:
union Test
{
int m;
float x;
char c;
}t1;
The above code declares a variable t1 of type union Test. This union contains three
members each with a different data type. However only one of them can be used at
a time. This is due to the fact that only one location is allocated for all
the union variables, irrespective of their size. The compiler allocates the storage
location which is equal to the size of its largest data member.
Accessing a Union Member in C
Syntax for accessing any union member is similar to accessing structure members,
union test
{
int a;
float b;
char c;
}t;
t.a; //to access members of union t
t.b;
t.c;
By Lec. Pratik Chand Page 24
C Programming – CSIT 1st Semester
Example:
#include <stdio.h>
union item
{
int a;
float b;
};
int main( )
{
union item it;
it.a = 12;
it.b = 20.2;
printf("a=%d\n", it.a);
printf("b=%.2f\n", it.b);
return 0;
}
Output:
a=1101109658
b=20.20
Here, the value of ‘a’ get corrupted and only variable ‘b’ prints the expected result.
This is because in union, all members share the same memory location. For better
understand see the following example again.
Example:
#include <stdio.h>
union test
{
int x, y;
};
By Lec. Pratik Chand Page 25
C Programming – CSIT 1st Semester
int main ()
{
// A union variable t
union test t;
t.x = 2; // t.y also gets value 2
printf("After making x = 2:\n x = %d, y = %d\n\n",t.x, t.y);
t.y = 10; // t.x is also updated to 10
printf("After making y = 10:\n x = %d, y = %d\n\n",t.x, t.y);
return 0;
}
Output:
After making x = 2:
x = 2, y = 2
After making y = 10:
x = 10, y = 10
Hence: - In union, all members share the same memory location.
Similarities between Structure and Union
Both are user-defined data types used to store data of different types as a
single unit.
Their members can be objects of any type, including other structures and
unions or arrays. A member can also consist of a bit field.
Both structures and unions support only assignment = and sizeof operators.
The two structures or unions in the assignment must have the same members
and member types.
A structure or a union can be passed by value to functions and returned by
value by functions. The argument must have the same type as the function
By Lec. Pratik Chand Page 26
C Programming – CSIT 1st Semester
parameter. A structure or union is passed by value just like a scalar variable
as a corresponding parameter.
‘.’ operator is used for accessing members.
Differences between Structure and Union
Program: - Difference between union and structure
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
By Lec. Pratik Chand Page 27
C Programming – CSIT 1st Semester
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Output:
size of union = 32 bytes
size of structure = 40 byte
Here, the size of union is = 32 bytes because it takes highest size variable only i.e.
name[32] all other variable share this memory. But the size of structure is 40 bytes
because name[32] = 32 bytes, int salary and int workerNo has 4, 4 bytes
respectively, so total size of structure is 40 bytes.
By Lec. Pratik Chand Page 28
C Programming – CSIT 1st Semester
Assignment:
1. What is structure? How is it different from array and Union? Discuss
2. What is structure? How is it different from array? Create a structure student
having data members name, roll-number and percentage. Complete the
program to display the name of student having percentage greater than or
equal to 60.
3. What is structure? Create a structure rectangle with data member length and
breadth, and display the area of rectangle.
4. What is structure? How is it different from union? Create a structure named
course with name, code and credit_hour as its member. Write a program
using this structure to read data of 5 courses and display data of those
courses with credit_hour grater than 3.
5. WAP to store the player name, runs scored, wicket taken of 20 cricketers
using structure then arrange the players in order of maximum wicket to
minimum wicket they had taken.
6. WAP to store the student name, age, marks of n students using structure then
arrange the students with their name in alphabetical order.
7. WAP to store the player name, runs scored, wicket taken of 20 cricketers
using structure along with pointer then arrange the players in order of
maximum wicket to minimum wicket they had taken.
Programs:
Q1. WAP to store the player name, runs scored, wicket taken of 20 cricketers using
structure then arrange the players in order of maximum wicket to minimum wicket
they had taken.
#include<stdio.h>
struct player
{
char name[20];
int runs;
By Lec. Pratik Chand Page 29
C Programming – CSIT 1st Semester
int wickets;
};
int i,j,n;
void main()
{
printf("Enter number of players:");
scanf("%d",&n);
struct player plr[n];
for(i=0;i<n;i++)
{
printf("\nEnter info of player %d\n",i+1);
printf("Enter Name:");
scanf("%s",&plr[i].name);
printf("Enter Runs:");
scanf("%d",&plr[i].runs);
printf("Enter Wicket Taken:");
scanf("%d",&plr[i].wickets);
}
sortplayers(plr);
printf("\nPlayers Status\n\n");
for(i=0;i<n;i++)
{
printf("Name: %s\n",plr[i].name);
printf("Runs: %d\n",plr[i].runs);
printf("Wickets: %d\n\n",plr[i].wickets);
}
}
void sortplayers(struct player sp[])
{
struct player temp;
By Lec. Pratik Chand Page 30
C Programming – CSIT 1st Semester
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((sp[j].wickets)> (sp[i].wickets))
{
temp= sp[i];
sp[i]=sp[j];
sp[j]=temp;
}
}
}
}
Q2. WAP to store the player name, runs scored, wicket taken of 20 cricketers using
structure then display the player info; who is taken wicket grater then 3.
#include<stdio.h>
struct player
{
char name[20];
int runs;
int wickets;
};
int i,j,n;
void main()
{
printf("Enter number of players:");
scanf("%d",&n);
struct player plr[n];
for(i=0;i<n;i++)
{
By Lec. Pratik Chand Page 31
C Programming – CSIT 1st Semester
printf("\nEnter info of player %d\n",i+1);
printf("Enter Name:");
scanf("%s",&plr[i].name);
printf("Enter Runs:");
scanf("%d",&plr[i].runs);
printf("Enter Wicket Taken:");
scanf("%d",&plr[i].wickets);
}
printf("\nPlayers Status\n\n");
for(i=0;i<n;i++)
{
if(plr[i].wickets>=3)
{
printf("Name: %s\n",plr[i].name);
printf("Runs: %d\n",plr[i].runs);
printf("Wickets: %d\n\n",plr[i].wickets);
}
}
}
Q3. WAP to store the student name, age, marks of n students using structure then
arrange the students with their name in alphabetical order.
#include <stdio.h>
#include<string.h>
struct student
{
char name[50];
int age;
float marks;
};
int i,j,n;
int main()
By Lec. Pratik Chand Page 32
C Programming – CSIT 1st Semester
{
struct student temp;
printf("Enter number of students:");
scanf("%d",&n);
struct student st[n];
for(i=0;i<n;i++)
{
printf("\nInfo for student %d\n",i+1);
printf("Enter your name:");
scanf("%s",&st[i].name);
printf("Enter your age:");
scanf("%d",&st[i].age);
printf("Enter your marks:");
scanf("%f",&st[i].marks);
}
sortStructure(st);
for(i=0;i<n;i++)
{
printf("\n\nInformation of student %d:\n",i+1);
printf("Name: %s\n",st[i].name);
printf("Age: %d\n",st[i].age);
printf("Marks: %.2f",st[i].marks);
}
return 0;
}
void sortStructure(struct student sst[])
{
struct student temp;
for(i=0;i<n-1;i++)
{
By Lec. Pratik Chand Page 33
C Programming – CSIT 1st Semester
for(j=i+1;j<n;j++)
{
if(strcmp(sst[i].name,sst[j].name)>0) // [(i-j)>0]
{
temp=sst[i];
sst[i]=sst[j];
sst[j]=temp;
}
}
}
}
Q4. WAP to store the player name, runs scored, wicket taken of 20 cricketers using
structure along with pointer then arrange the players in order of maximum wicket
to minimum wicket they had taken.
#include<stdio.h>
struct player
{
char name[20];
int runs;
int wickets;
};
int i,j,n;
void main()
{
printf("Enter number of players:");
scanf("%d",&n);
struct player plr[n];
struct player *ptrplr;
ptrplr = plr;
for(i=0;i<n;i++)
By Lec. Pratik Chand Page 34
C Programming – CSIT 1st Semester
{
printf("Enter info of player %d\n",i+1);
printf("Enter Name:");
scanf("%s",&(ptrplr+i)->name);
printf("Enter Runs:");
scanf("%d",&(ptrplr+i)->runs);
printf("Enter Wicket Taken:");
scanf("%d",&(ptrplr+i)->wickets);
}
sortplayers(ptrplr);
printf("\nPlayers Status\n\n");
for(i=0;i<n;i++)
{
printf("Name: %s\n",(ptrplr+i)->name);
printf("Runs: %d\n",(ptrplr+i)->runs);
printf("Wickets: %d\n",(ptrplr+i)->wickets);
}
}
void sortplayers(struct player *sp)
{
struct player temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((sp+j)->wickets > (sp+i)->wickets)
{
temp= *(sp+i);
*(sp+i)=*(sp+j);
*(sp+j)=temp;
}
By Lec. Pratik Chand Page 35
C Programming – CSIT 1st Semester
}
}
}
End of Unit - 8
By Lec. Pratik Chand Page 36