Hi translation of C language - structures, unions and bit fields (5)

1. const char* means that the string literal value will be passed, mainly saving the string that you do not want to modify.
2. Struct is the abbreviation of structured data type, which can write different types of data together.
3. The size of the structure is fixed, and the data in the structure has names.
4.

struct fish snappy = {
    
    “Snappy”,“Piranha”,694}

struct fish is a data type.
snappy is the variable name.
5. Encapsulate the parameters in a structure, and the code will be more stable.
6. When accessing a structure, you cannot access it in the same way as an array. You should use the "." operator to access the structure fields.
For example:

printf("Name=%s\n",snappy.name);

7. Note: When defining the structure, add ";" at the end.
8. Copy the structure. Specifically, the string pointer is copied, not the string itself.
9. Perform structural operations:
(1) Define structural data
(2) Use structural data to substitute directory items (it is recommended to rename the structure)
(3) Assign values ​​to the structure and use directory output.
Insert image description here

10. Use typedef to name the structure. The rule is to add typedef before the structure and give an alias after it.
Insert image description here

11. The alias is the type name, which means that the structure has two names, one is the structure name (struct cell_phone), and the total is the type name (phone). 12. The structure is a
data type composed of a series of other data types.
13. When updating the structure, fields can be modified like variables.
14. When you want to pass a structure to a function and update its value, you need to use a structure pointer.
15. (*t).age points to the age *t.age refers to the content in the memory unit t.age.
16.

t->age==*t).age

17. Unions can use memory space efficiently.
Insert image description here

18. How to use union:
(1) C89 method:

 quantity q = {
    
    4}

(2) Specify initializer:

quantity q = {
    
    .weight=1.5};

(3) "Point" representation:

quantity q;
q.volume = 3.7

19. Union is often used together with structure. Values ​​of various types can be saved in a union, but after saving, it is not known in which type the values ​​are saved. So you can use a trick - create an enumeration.
20. Enumeration: (data items are separated by commas)

enum colors{
    
    RED,GREEN,PUCE};
enum colors favorite = PUCE;

21. The bit field can specify how many bits there are in the total field. Generally, the bit fields are continuous and put together.
Insert image description here

22. Why does C language not support binary literal values?
Because binary literals take up a lot of space, and hexadecimal is faster to write.

Guess you like

Origin blog.csdn.net/weixin_46504000/article/details/129245097