Getting started with iOS development☞C language (struct, Consortium, enumeration), ios Consortium
Struct 1. Concept
Struct: custom data type.
Struct variables are composite variables composed of multiple other variables. The types of multiple variables can be different, and each variable has its own name.
2. struct Definition
- General definition method:
- Common definition methods:
3. Initialization of struct Variables
4. assign values (assign values one by one)
There are two types of access to struct member variables: one is to use the symbol "." (generic), and the other is to use the symbol "->" (generally used for struct pointer variables ).
5. Memory occupied by struct
About the structure memory alignment rules: # The macro pragma pack (n) indicates that it is set to n-byte alignment (8-byte alignment by default for VC6)
- For each member of the structure, the first member is located at the offset of 0, and the offset of each data member must be a multiple of min (n, size of the member. (For example, if the int value is 4 bytes on a 32-bit server, it must be stored at an integer multiple of 4 ).
- After the data members are aligned, the structure (or union) itself needs to be aligned. The total size must be a multiple of min (n, maximum member size.
- Struct as a member: If a structure contains some struct members, the struct members should be stored from an integer multiple of the maximum internal element size. (Struct a contains struct B, B contains char, int, double and other elements, and B should be stored as an integer multiple of 8 .)
First, it is clear that the Members in the struct are arranged from the low address to the high address, which is similar to the array, that is, the members in front of the struct occupy the low memory address, the member behind the struct occupies a high memory address.
6. construction type: array/struct
- Array: used to save a group of data of the same type.
- Struct: used to save a set of different types of data.
Consortium union1.
Union, the Chinese name is "consortium, sharing body ". The usage, syntax, and struct of a consortium are very similar.
- The size of memory space occupied by the consortium is equal to the number of bytes occupied by the longest member.
- All members in the Union share the same memory space. (The Consortium uses the coverage technology. Several member variables overwrite each other, so that several different variables share the same memory)
- A consortium can only store values of one member variable. (Assign a value to the new member variable to overwrite the value of the original member variable)
- The order in which union members are stored starts from the low address.
2. Definition
Union syntax format: (same as struct)
// In the shared body data, the maximum space occupied by Member I is 4 bytes, so the data type variables (that is, a, B, c) it also occupies 4 bytes of memory.
- Assigning values to a member overwrites the values of other members (because they share a piece of memory. However, the premise is that the number of bytes occupied by members is the same. When the number of bytes occupied by members is not the same, only the values of the corresponding bytes will be overwritten. For example, if a char member is assigned a value, the entire int member will not be overwritten, because char occupies only one byte, and int occupies four bytes)
- Why should we introduce a consortium:
The memory space allocation of the struct is extensive, regardless of the need, full allocation; the memory usage of the consortium is more refined and flexible, saving memory space.
Enumeration definition:
The C language provides an enum type to list all possible values. The format is as follows:
Enum enumeration name {identifier 1 [= integer constant], identifier 2 [= integer constant],... identifier n [= integer constant]};
Note: The enumerated values are identifiers. Note the naming format!