2. CONTENTS :
Introduction of structure
Array of structure
Nested structure
Structure with pointer
3. Introduction -
In C/C++, struct keyword is used to define
a structure, a structure is the collection of
variables with a common name. Structure
variables are called fields. A structure is
heterogeneous in that it can be composed
of data of different types. Structure is an
user defined data type.
8. The members of a structure are accessed with
the dot (.) operator.
syntax – structure_var_name.fieldname;
: st.rollno;
st.st_name;
st.per_marks;
9.
10. In contrast, array is homogeneous since it can
contain only data of the same type.
Complex data structures can be formed by
defining arrays of structs.
syntax – struct structure_name array_name[i];
For Exa : struct student_type st[50];
13. When a structure whose member also of a
structure type then such structure are called
Nested structure.
Nested are of two types –
1) Separate Structure
2) Embedded Structure
1) : We can create two
structures, but dependent
structure should be used inside the main
structure as a member.
14. struct date{
int dd;
int mm;
int yyyy;
}doj;
struct employee{
int id;
char name[20];
struct date doj;
}emp1;
15. 2) Embedded Structure : In this structure,
we can define structure
within the structure also.
struct employee{
int id;
char name[20];
struct date{
int dd;
int mm;
int yyyy;
}doj;
}emp1;
16. Structure with Pointer -
Pointer variable can point to the address of a structure
variable.
For Exa – struct dog{
char name[10];
char breed[10];
int age;
char colour[10];
}spike;
struct dog my_dog={“tyke”,”Bulldog”, 5, “White”};
struct dog *ptr_dog;
ptr_dog=&spike;
17. When we want to access member of the
structure through pointer, arrow (->)
operator is used.
For Exa –
ptr_dog->name;
ptr_dog->breed;
Or –
(*ptr_dog).name;
(*ptr_dog).breed;
18. For Exa –
struct point{
int x;
int y;
}point;
int main()
{
Point P;
printf(“Enter the coordinates of point”);
input(&P);
output(&P);
}