Structure:
- Structure is the derived data type.
- Structure is the collection of multiple data elements that can be of different data types.
Syntax:
Struct structure_name{
data_type variable_name;
data_type variable_name;
-
-
}
Example:
Struct student{
int roll_number;
char name[25];
float attend;
};
Nested Structure:
- If one of the members of structure is also a structure, then such a structure is called as a nested structure.
- The syntax of a nested structure can be given below:
Syntax:
Struct structure_name {
data_type variable_name;
struct {
data_type varaible_name;
}
internal_structure_name;
};
Example:
struct circle {
float radius;
struct {
float x, y;
}
center;
}
Structure for given description:
Struct cricketer {
char name[20];
int age, matches_played, runs;
float average;
};
Explanation:
- The memory space required for a “char” type data is 1 byte.
- For “int” data type it requires 2 bytes and for “float” 4 bytes.
- Thus, 20 bytes will be required for name, 2 bytes each for age, matched_played and runs, while 6 bytes for average.
- Hence the total space required for the variable created of structure “cricketer” will be 20 + 2 + 2 + 2 + 4 = 30 bytes.