01 02 03
Introduction to structures Array of structures Nested structures
01
Why we need Structures ?
As we know an array can store a collection of same data type such as int, char,
or double . But what if we wish to store a character with an integer variable.
Can a Arrray handle that ? Answer is No, because it can only store variables of
same data type. Here Structure comes into picture. Let’s discuess about
structure in C.
Structures in C
• Structure is basically a user defined data type .
• It can store related information (even of different data types) together
under a single name (structure tag name).
• The variable within a structure may be of different data types and each has
a name called as member of structure.
Declaring a structure
• A structure is declared by using the keyword struct followed by an optional structure tag(structure
name) followed by the body of the structure.
• Syntax:-
Don’t forgot
struct <structure_name> {
to add a
data type variable_name_1; semicolon after
data type variable_name_2; the curly
braces
............................................
............................................
data type variable_name_n;
};
• Once structure_name is declared as new data type, then variable of that type can be declared as:
struct structure_name structure_variable ;
• Note:- The structure declaration doesn’t allocate any memory space.
• Example:-
struct student{ struct
char name[20]; student
int roll_no; stud_1
char gender;
long int phone_no;
};
struct student stud_1;
• Here is another way of declaration of variable where variable is declared at the
same time of structure declaration. Multiple structure variable can be declared
using comma(,).
struct student{
char name[20];
int roll_no;
char gender;
long int phone_no;
}student stud_1, stud_2;
• The use of structure_name is optional.
struct
{
char name[20];
int roll_no;
char gender;
long int phone_no;
}student stud_1, stud_2;
Typedef Declaration
• The typedef is used to create a new data type name from an existing data
type.
• General syntax of using typedef is:
typedef existing_data_type new_data_type;
• Ex:-
typedef struct student{
char name[20];
int roll_no;
char gender;
long int phone_no;
}stud;
stud stud_1,stud_1;
Accessing the member of structure
• The members of a structure can be accessed by relating them to structure variable with a
dot operator (.) . The general form will be:
<structure_name> . <structure_name> ;
Ex:- In
struct point {
int x;
int y;
} a, b ;
the first member can be acessed by the construct a.x . Similarly second member can be
acessed by a.y for the variable ‘a’ .
• Each variable of structure has its own copy of member variables.
a b
x x
y y
structure initialization
• A structure can be initialized in same way as any other data types.
• The general construct for initialization is:
struct <structure_name> {
data type variable_name_1;
data type variable_name_2;
............................................
............................................
data type variable_name_n;
}<structure_variable> = {constant_1, constant_2,......,constant_n};
• It can olso be initialised later after declaration.
struct <structure_name> <structure_variable> = {constant_1, constant_2,......,constant_n};
Ex:-
#include <stdio.h>
struct student{
char name[20];
int roll_no;
float mark;
}std1={“ram”,39,98.5};
int main(){
printf(“Name:%s\nRoll no:%d\n/Mark:%f”,std1.name,std1.roll_no,std1.mark};
return 0;
}
output:-
Name:ram
Roll no:39
Mark:98.5
• We can iniitialize the first few members and leave the remaining
uninitialized. It is called partial initialization.
• The uninitialized members are assigned by default to zero for
integer and floating point no. and ‘\0’ (null character) for
character and string.
Ex:-
#include <stdio.h>
struct student{
char name[20];
int roll_no;
float mark;
}std1={“ram”,39};
int main(){
printf(“Name:%s\nRoll no:%d\n/Mark:%f”,std1.name,std1.roll_no,std1.mark};
return 0;
}
output:-
Name:ram
Roll no:39
Mark:0
• A structure can be assigned to another structure of the same type.
Ex:-
#include <stdio.h>
struct student{
char name[20]; output:-
Name:ram
int roll_no;
Roll no:39
float mark; Mark:95.00
};
int main(){
struct student std1={“ram”,39,95.00};
struct student std2;
std2=std1; //copying respective members of std1 to std2
printf(“Name:%s\nRoll no:%d\n/Mark:%f”,std1.name,std1.roll_no,std1.mark};
return 0;
}
• C doesn’t permit to use comparison operation on structures.
i.e std1==std2;
std1!=std2;
are not permitted.
• But we may compare individual members of two structures.
i.e std1.roll_no == std2.roll_no
02
Array of structures
• Let’s consider a structure:
struct student {
char name[20];
int roll_no;
float mark;
};
• If we wish to keep a data of 100 students, we have to declare
100 structure variables like
struct student stud1,stud2 ,...........,stud100;
• In this case we have to use an arrray to store the information of
100 students.
• An array of a structure can be declared as:
struct student {
char name[20];
int roll_no;
float mark;
}stud[100];
Here 100 is the no of elements array created.
• The above structure create a array of 100 variable which stores their
individual copies of structure members.
struct student struct student struct student
stud[0] stud[1] stud[100]
• name • name ....... • name
• roll_no • roll_no • roll_no
• mark • mark • mark
initializing array of structure
• The initialization of array of structure can be carried out in much same way as array of standard data types.
• The general construct will be:
struct <structure_name> {
data type variable_name_1;
data type variable_name_2;
............................................
............................................
data type variable_name_n;
};
struct <structure_name> <structure_variable> [N] = {
(constant_01, constant_02,......,constant_0n),
(constant_11, constant_12,......,constant_1n),
................................................................
................................................................
(constant_N1, constant_N2,......,constant_Nn)
};
• Ex:- #include<stdio.h>
struct customer { output:-
char name[20]; boarder seat no fare
sri 1 10.00
int seat_no;
disha 2 15.00
float fare; roy 3 20.00
};
int main(){
struct customer ticket[3]={(“sri”,1,10.00),(“disha”,2,15.00),
(“roy”,3,20.00)};
printf(“boarder\tseat no\tfare”);
for(int i=0;i<=2;i++)
printf(“\n%s %d %f ”,ticket[n].name,ticket[n].seat_no,
ticket[n].fare);
return 0;
}
03
Nested structures
• A structure can be placed within another structure. In other words, structure can contain other structure as
members.
• An example of nested structure declaration is given below:
struct outer { struct inner{
int out1; int inn1;
float out2; float inn2;
struct inner { (or) }innvar;
int inn1; struct outer {
float inn2; int out1;
}innvar; float out2;
}outvar; struct inner innvar;
}outvar;
• The innermost members can be acessed by chaining all the concerned structure variables, from
outermost to innermost, with the member using the dot operator.
• i.e
outvar.innvar.inn1=10;
outvar.innvar.inn2=50.50;
#include<stdio.h>
int main(){
struct birthday { output:-
int day;
Name:sahil
DOB:17/12/2004
int month;
int year;
};
struct member {
char name[20];
struct birthday DOB;
};
struct member member1;
member1.name=”sahil” ;
member1.DOB.day=17;
member1.DOB.month=12;
member1.DOB.year=2004;
printf(“Name:%s\nDOB:%d/%d/%d”,member1.name,member1.DOB.day,member1.DOB.month,
member1.DOB.year);
return 0;
}