Structure 4
Structure 4
Structures: Basics of Structures, Nested Structures, Arrays of Structures, Arrays within structures,
Structures and functions, pointers and structures, Self-referential structures, Unions.
Introduction to Structure
• Problem:
• – How to group together a collection of data items of different
types that are logically related to a particular entity???
Structure
•A is a collection of variables of
structure
• different data types under a single name.
• Thevariables are called members of the structure.
• The structure is also called a user-defined data type.
3
Defining a Structure
• Syntax:
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
………………………………;
data_type member_variableN;
};
Once structure_name is declared as new data type, then
variables of that type can be declared as:
struct structure_name structure_variable;
Note: The members of a structure do notoccupy
memory until they are associated with a structure_variable.
4
• Example
struct student
{
char name[20];
int roll_no;
float marks;
char gender;
long int phone_no;
};
6
Defining a
structure…
• The structure and The use of structure_name is
variable declaration can
definition be optional.
combined as: struct
struct student {
{ char name[20];
char name[20]; int roll_no;
int roll_no; float marks;
float marks; char gender;
char gender; long int phone_no;
long int phone_no; }st1, st2, st3;
}st1, st2, st3;
7
ACCESSING MEMBERS OF STRUCTURE :
To access the members of the structure, a variable for the structure
has to be created.
Syntax :
Struct tag variablename;
ARRAYS STRUCTURES
struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN;
};
for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&S.Marks[i]);
In the above example, we have created an
array Marks[ ] inside structure representing S.Total = S.Total + S.Marks[i];
}
3 marks of a single student. Marks[ ]
is now a member of structure student and to S.Avg = S.Total / 3;
access Marks[ ] we have used dot operator(.)
printf("\nRoll : %d",S.Roll);
along with object S. printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
printf("\nAverage : %f",S.Avg);
}
Output :
Roll : 10
Name : Kumar
Total : 223
Average : 74.00000
Write a C program to maintain record of N Employee detail using array of structure with 3
fields: ID, Name and Salary And print details of employees whose salary is above 10000.=
int main()
#include<stdio.h> {
printf(“The Employee Details:\n”);
printf(“Eid \t Name \t Salary\n”);
struct employee for (i=0;i<n;i++)
struct employee e[10];
{ printf(“%d \t %s \t %d \n”,e[i].eid,e[i].name,e[i].salary);
int n,i;
int eid;
printf(“Enter the no.of employees\n”);
printf(“Employee salary above 10000\n”);
char name[20]; scanf(“%d”,&n);
for(i=0;i<n;i++)
int salary; {
printf(“Enter the Employee details:\n”);
if(e[i].salary>10000)
}; for(i=0;i<n;i++)
printf(“%s\n”,e[i].name);
{
}
printf(“Enter %d employee detail:\n”);
}
printf(“\nEnter the EID: ”);
scanf(“%d”,&e[i].eid);
printf(“\nEnter the Name: ”);
scanf(“%s”,e[i].name);
printf(“\nEnter the salary: ”);
scanf(“%d”,&e[i].salary);
}
Enter the no.of employees Enter the salary: 2000 The Employee Details:
3 Enter 3 employee detail: Eid Name Salary
11 arun 12000
Enter the Employee Enter the EID: 33 22 rao 2000
details:
33 bharath 25000
Enter 1 employee detail: Enter the Name: bharath Employee salary above 10000
arun
Enter the salary: 25000 bharath
Enter the EID: 11