Topics in Structures:-
Ordinary variables can store only one value at a time where as array stores a collection of
similar data type elements.
But sometimes it is necessary to store information of different data types as a single unit.
Ex: Student details contains Student no, student name, student average
Book details contains Book name, book price, book author, book code
Structure definition:
--------------------------------
};
structure name or tag name:- For structure name identifier rules are to be followed.
structure name is nothing but name of the user defined data type.
data type:- The basic data types are int, float, char and double.
members:- The variables which are declared inside a structure are known as members. The
members of a structure must be enclosed in curly braces { }.
struct student
int rno;
char name[20];
int m1,m2,m3;
};
Whenever a structure is defined then memory will not be allocated to the members of
the structure.
For member variables the memory will be allocated only when a structure variable is
declared.
So, whenever a structure variable is declared then memory will be allocated to the
members of the structure.
Like normal variables the structure variables are declared before they are used in the
program.
struct structure_name
datatype1 var1,var2;
datatype2 var1,var2;
------------------------
------------------------
}structure_variable1, structure_variable2,…………structure_variablen;
Example:
struct student
int no;
char name[20];
float avg;
}s1,s2;
struct structure_name
datatype1 var1,var2;
datatype2 var1,var2;
------------------------
------------------------
};
1.Using period or dot(.) operator:- If the structure variable is an ordinary variable then we
uses dot(.) operator for accessing the members of structure.
Syntax:- structure_variable.member;
Ex:- s1.rno;
s1.name;
s1.m1;
Storing the values in the members of structure:- We can store the values in the members
of structure in 2 ways.
s1.rno=10;
s1.m1=90;
strcpy(s1.name,”ramu”);
Eg:- scanf(“%d%d%d%d%s”,&s1.rno,&s1.m1,&s1.m2,&s1.m3,&s1.name);
2.Using Arrow(->) operator:- If the structure variable is pointer variable then we uses
arrow(->) operator for accessing the members of structure.
Syntax:- structure_variable->member;
Ex:- s1->rno;
s1->name;
s1->m1;
Storing the values in the members of structure:- We can store the values in the members
of structure in 2 ways.
s1->rno=10;
s1->m1=90;
strcpy(s1->name,”ramu”);
Eg:- scanf(“%d%d%d%d%s”,&s1->rno,&s1->m1,&s1->m2,&s1->m3,&s1->name);
Student structure example or write a c program to access the student data with fields
rno, name, marks and display the student details.
#include<stdio.h>
int main()
struct student
int rollno;
char name[20];
int m1,m2,m3,total;
float avg;
}s1;
scanf("%d",&s1.rollno);
printf("\nenter name");
fflush(stdin);
gets( s1.name);
scanf("%d%d%d",&s1.m1,&s1.m2,&s1.m3);
s1.total=s1.m1+s1.m2+s1.m3;
s1.avg=(float)s1.total/3;
return 0;
Result:-
Input:-
enter roll no 10
Output:-
Roll no=10
Name=ramu
M1 marks=70
M2 marks=80
M3 marks=90
Total=240
Average=80
Arrays of structures:-
Syntax2:-
struct structure_name
datatype1 var1,var2;
datatype2 var1,var2;
------------------------
------------------------
}structure_variable1[size], structure_variable2[size],…………structure_variablen[size];
Ex2:-
struct student
int no;
char name[20];
float avg;
}s1[10],s2[10];
- --
- --
- --
#include<stdio.h>
int main()
struct emp
int empid,sal;
char name[20];
}e[10];
int i,total=0,n;
float avg;
printf("\nenter no of employees");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&e[i].empid);
scanf("%d",&e[i].sal);
fflush(stdin);
for(i=0;i<n;i++)
total=total+e[i].sal;
avg=(float)total/n;
for(i=0;i<n;i++)
printf("\nemp id is %d",e[i].empid);
return 0;
Result:-
Input:-
enter no of employees 2
enter emp id 10
enter emp id 20
Output:-
emp id is 10
emp id is 20
Arrays within structures:- An array is a collection of similar data type elements and all the
elements will be stored in adjacent memory locations. We can use arrays with in the
structure.
Ex:-
#include<stdio.h>
int main()
struct student
int rno;
char name[20];
int m[6];
int total;
}s[10];
int i,j,n;
printf("\nenter no of students");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&s[i].rno);
printf("\nenter name\n");
fflush(stdin);
gets(s[i].name);
printf("\nenter marks\n");
for(j=0;j<6;j++)
scanf("%d",&s[i].m[j]);
for(i=0;i<n;i++)
s[i].total=0;
for(j=0;j<6;j++)
s[i].total=s[i].total+s[i].m[j];
}
for(i=0;i<n;i++)
printf("\nroll no is %d",s[i].rno);
printf("\nname is %s",s[i].name);
printf("\nTotal is %d",s[i].total);
return 0;
Result:-
Input:-
enter no of students 2
enter rno 10
enter marks 90 91 92 93 94 95
enter rno 20
enter marks 80 81 82 83 84 85
Output:-
rollno is 10
name is ramesh
Total is 555
rollno is 20
name is naresh
Total is 495
Nested structures:-
struct student
int no;
char name[20];
int total;
}s={10,”ramu”,550};
Ex:- C program to accept 2 dates from the user and check if they are different dates or same.
#include<stdio.h>
main()
struct date
int day,month,year;
};
clrscr();
else
getch();
Output:-
struct student
int rno;
char name[20];
int marks;
};
void main()
ptr->rno=10;
strcpy(ptr->name,"ramu");
ptr->marks=550;
printf("\nname is %s",ptr->name);
printf("\nrno is %d",ptr->rno);
printf("\nmarks is %d",ptr->marks);
#include<stdio.h>
void display(int,char[]);
int main()
struct student
int rno;
char name[20];
}s;
scanf("%d",&s.rno);
fflush(stdin);
printf("\nEnter name");
gets(s.name);
display(s.rno,s.name);
return 0;
printf("\nroll no is %d",rno);
printf("\nname is %s",name);
Result:-
Output:- roll no is 10
name is ramesh
2.Passing entire structure as argument to a function:-
#include<stdio.h>
struct student
int rno;
char name[20];
}s;
int main()
scanf("%d",&s.rno);
fflush(stdin);
printf("\nEnter name");
gets(s.name);
display(s);
return 0;
printf("\nroll no is %d",s.rno);
printf("\nname is %s",s.name);
Result:-
Output:- roll no is 10
name is ramesh
#include<stdio.h>
#include<conio.h>
struct complex
int real,imag;
}c1,c2,c3;
int main()
scanf("%d%d",&c1.real,&c1.imag);
scanf("%d%d",&c2.real,&c2.imag);
add(c1,c2);
subtract(c1,c2);
return 0;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
printf("\nAddition=%d+i%d",c3.real,c3.imag);
}
void subtract(struct complex c1,struct complex c2)
c3.real=c1.real-c2.real;
c3.imag=c1.imag-c2.imag;
printf("\nSubtraction=%d-i%d",c3.real,c3.imag);
Result:-
Output:-
Addition=6+i10
Subtractiob= 2-i2
Linked list:-It is a collection of nodes which are not necessary to be in adjacent memory
locations.
1. Data Field
Data field:- Data field contains values like 9, 6.8, ‘a’ , “ramu” , 9849984900
Next field:- It contains address of its next node. The last node next field contains NULL
which indicates end of the linked list.
#include<stdio.h>
main()
struct node
int data;
};
scanf("%d",&first->data);
scanf("%d",&second->data);
printf("\nThird node\n");
scanf("%d",&third->data);
first->address=second;
second->address=third;
third->address=NULL;
ptr=first;
while(ptr!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->address;
Result:-
Output:- 10->20->30
--------------------------------------------------------------------------------------------------------------