Advanced C language Notes 4 points

  • Five structures

1, definitions and concepts

struct structure type name

{

         Member list

};

You can be redefined first described, and then they may be defined before the semicolon

Definition of the structure before the semicolon variable, while omitting the structure type name , it is no longer defined after such a structure variable is defined in this case only, the use of a limited number of commonly used global variables

typedef struct (structure type name optional)

{

       Member list

} Type name ; semicolon before the variable is no longer defined

To define a variable simply type the name plus the variable name  

Variable name assignment points plus Member Name

When the structure explained that the list of members can make another structure, can be used when the multi-level references

The same type of structure variables can be directly assigned to one another

2, array of structures

Array is an array of structures, a collection of a plurality of the same type of structure variable composition

struct structure type name array name [element number];

3, a structure pointer

Pointers to structures and methods for defined variables and the general assignment as

The overall structure and also references the same as ordinary variables

But when referring to members of different structures, need to add brackets. Higher priority than *, (* ps) .num

Bracketed trouble, can be used to point the operator (->) in place, ps-> num

When nested, the members inside the reference structure, the first layer a second layer with the operator pointing the dot operator

4, the structure memory allocation

Value at the specified alignment value #pragma pack (value).

value only: 1248, etc.

Byte alignment, the current value is an integer multiple of the members remains unchanged; value is less than the current member, but after adding a member happens to be an integral multiple of the same value; value is less than the current member, together with the latter member is also smaller than the integer value times the value of the integral multiples up.

Sizeof can be used to measure the space occupied

5, bit field (bit field)

struct packed_data
{
    unsigned int a:2;
    unsigned int b:6;
    unsigned int c:3;
    unsigned int  :1; // 无意义的位段
    unsigned int d:4;
    unsigned int i;
} data;

Colon behind the numbers that make up a few, can not take the address of a bit segment members

Do not exceed the range defined by the assignment bit segment, will range assignments interception operation

You can define meaningless bit segment, simple space, in order to ensure that members needed to occupy the back

The same structure, the member may include a variable bit segment and common member variables simultaneously


  • Six, union

Similar structures and union, also a type of data structure configuration

The method defined in common body also defines a method and structure very similar to the struct into a union, as another method of use, but does not allow the bit segments

When performing certain algorithms, several different types of variables need to be stored in the same memory cell section, a space of several variables used in overlap

Body size of the common memory is that it accounts for the size of the maximum length of the member

Members of a union at the same time only an effective, commonly used in the process of sending the network protocol


  • Seven, enumerated types

The value of the variable within a range of eleven enumerated value of the variable is limited to the enumerated value

enum enumeration type name

{

         Enum values list (separated by commas members)

};

Typedef can also be used

With a local enumeration can be used instead of the macro definition, not vice versa

Enum members can be any content, the actual value is an integer incremented from zero by default, it may be changed by an integer value corresponding member to the member assignment

The definition of enumeration: enum enumeration type enum name names

Released four original articles · won praise 1 · views 72

Guess you like

Origin blog.csdn.net/LF_1845529754/article/details/104025323