Structs also belong to constructed types. Structs are more flexible than arrays and can hold different types of variables
A struct is made up of groups of members that can be either basic data types or constructed types, such as arrays
struct belongs to custom data type
1. Structure declaration :
struct Structure Body name
{
type specifier member name;
...;
type specifier member name;
}; Note that this number cannot be reduced.
A struct is a custom type, and a struct variable is a variable that is decorated by a struct type.
Defining struct-Body variables
struct struct body name Variable name = {initial value};
struct member access
Representation of struct members
struct variable, member
typedef creates a new name, or type alias, for an existing type
typedef primitive Type New type
typedef int INTEGER
Such
int a = 10;
Can be written as:
Integer a = 10;
struct variable can be directly assigned value
Attention
Although arrays and structs are constructed types, struct variables can be assigned directly, and arrays cannot be directly assigned values.
Tips:
You can implement the direct assignment of an array by placing the array in the structure body
Structure Body Storage :
The space occupied by the structure can simply be considered as the sum of the space of the memory occupied by each member.
is actually the smallest integer multiple of the space occupied by the largest member variable
Arrays are split aligned when the number of bytes in this constructed type of array is greater than the bytes of the largest data type in the struct member variable
The essence of the memory alignment principle in a computer is for the CPU to be able to effectively access the relevant memory through the address.
Structure nesting
A member of a struct can still be a struct body
struct array
Multiple struct variables can be placed into an array, constituting an array of structs.
Example: struct Stu students[10]
Each struct variable can be accessed by subscript
C-language Classic---structural body