Зміст курсу
C Structs
C Structs
Defining and Declaring Structs
Defining
A structure declaration begins with the keyword "struct".
python
<type_1> <field_1>
- variables that will make up the structure.
The structure declaration does not take up memory, that is, it is just a drawing, a template.
First way
Once we have declared a structure, we can create a variable of that type using the word struct
.
python
Declaring multiple struct variables is the same as declaring regular variables:
python
Second way
This option allows the structure to be instantiated immediately after it is defined.
python
In this way it is convenient to create several variables at once:
python
Spoiler method
The use of the typedef
keyword is also used in creating structures:
python
Note
The use of the "typedef" keyword will be covered later in this course.
Initialization
Initializing a structure == initializing a variable of a new data type
To initialize a structure, you must use curly braces {...}
:
python
In this case, the fields will be assigned in order and there is no need to indicate the data type of each field, because this has already been done in the blueprint of the structure.
Дякуємо за ваш відгук!