Structure
Structure
C Structures
The structure in C is a user-defined data type that can be used to
group items of possibly different types into a single type. The struct
keyword is used to define the structure in the C programming
language. The items in the structure are called its member and they
can be of any valid data type.
2
C Structures
3
C Structure Declaration
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
The above syntax is also called a structure template or structure prototype and no memory is
allocated to the structure in the declaration.
C Structure Definition
To use structure in our program, we have to define its instance. We can do that by
creating variables of the structure type. We can define structure variables using two
methods:
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
The reason for the above error is simple. When a datatype is declared, no memory is allocated for it. Memory is
allocated only when variables are created.