4. What is structure
A structure is a user defined data type that can be used to group items of different
data types into a single type.
Example:
struct student
{
char name[20];
int age;
int ID Number;
float CGPA;
};
5. Advantage of structure
An array can also store multiple data but only of one
data type. But using structure has the advantage of
storing multiple data of different data type.
6. Format of a structure definition
Struct tag_name
{
data_type member1;
data_type member2;
data_type member3;
……………
};
Struct student
{
char name[40];
int age;
int CGPA;
……………
};
7. Elements of structure definition
The keyword struct
The structure tag name
A terminating semicolon
9. Structure initialization
We can initialize structure in two ways-
struct student
{
char name[40];
int age;
int ID;
float CGPA;
}student={“Nusrat”,25,121,3.94};
struct student
{
char name[40];
int age;
int ID;
float CGPA;
};
Struct student s1 ={“Nusrat”,25,121,3.94};
10. Rules for initializing structure
We can’t initialize individual members inside structure
template.
The order of values in braces must match the order of
members in the structure definition.