C language learning [8]-structure

1. Structure

The C array allows you to define variables that can store the same type of data items. The structure is another user-defined available data type in C programming. It allows you to store different types of data items. The structure is used to represent a record.

The struct statement defines a new data type that contains multiple members. The format of the struct statement is as follows:

struct tag {

member-list

member-list

member-list

...

} variable-list ;

tag is the structure tag

member-list is a standard variable definition

Variable-list is a structure variable, defined at the end of the structure, before the last semicolon, one or more structure variables can be specified.

for example:

struct Books{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

@Under normal circumstances, at least two of the three parts of tag, member-list, and variable-list must appear

@ Like other types of variables, the initial value of the structure variable can be specified at the time of definition

@ To access the members of the structure, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define variables of structure type​​​​

@You can use structure as a function parameter, and the way of passing parameters is similar to other types of variables or pointers

@ Can define pointers to structures, in a similar way to defining pointers to other types of variables

 

2. Supplement: bit field

When some information is stored, it does not need to occupy a complete byte, but only a few or one binary bit. For example, when storing a switch value, there are only two states of 0 and 1, just use 1-bit binary. In order to save storage space and make processing easier, C language provides a data structure called "bit field" or "bit segment".

The so-called "bit field" is to divide the binary bits in a byte into several different areas, and specify the number of bits in each area. Each domain has a domain name, which allows you to operate by domain name in the program. In this way, several different objects can be represented by a one-byte binary bit field.

The use of bit fields is the same as the use of structure members, and its general form is:

Bit field variable name. Bit domain name 
Bit field variable name -> Bit domain name

 

 

 

Guess you like

Origin blog.csdn.net/qq_46009608/article/details/115289014