Structuret
Structuret
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 .
• 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 ;
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};
• 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: