structs, enumerations, unions

structure

1. The creation of the structure

(1) Before constructing a structure, the structure must be declared.

     Example: struct student // Describe a student, student is the label

{   char name[20];

    Int age;

    char sex[5];// The content cannot be empty

}; // The semicolon here cannot be lost, and variables can also be written here

(2) Member access

2.1 struct student{} in ( 1 ) ; equivalent to type, such as int type,

Struct student s ;// s is a variable

s has name, age, sex, three members;

The access member is in the form of ( s. member): Example: s.age: access is the age member

  

2.2 If you need to access the structure variable pointed to by the pointer, you should dereference the pointer, so that you will get the content pointed to by the pointer.

     Example: p is a pointer to a structure variable

          Struct student *p=s;

(*p).name   (p->name)

  // Access members , both forms can be

 

(3) Initialization

  Example: (1)struct student n1={“henry”,20, “male” };// First declare the structure, then assign

     (2) Struct student{...}n2={"henry",20,"male"};// Define variables and initialize them at the time of declaration.

2. Memory alignment of structures

   When declaring members in a structure, the members must follow the principles of memory alignment when they are stored in memory: platform reasons and performance reasons.

   1struct s1

         { char c1;// The address offset of the first member is 0 , so no need

                     / align, occupy one byte

              Int i;// Alignment should be considered for the second member, because the int type occupies 4 bytes, and it needs to be stored at an integer multiple of its own alignment number, so leave three bytes after the first member, so that can be stored

              Char c2;// A total of 8 bytes are stored, and char is 9 , but 9 is not an integer multiple of the maximum alignment number of 4 , so three bytes are added, and the result is 12 .

}

(2)struct s2

                { char c1;// It is 1 , add 3

                      Struct s1 s3;// The maximum alignment number in s1 is 4 , then its alignment number is 4 , and its size is 12

                      Double d;// The alignment number is 8, 16 is an integer multiple of 8, so it is 24 ; 24 is an integer multiple of the maximum alignment number 8 , so the result is 24

}

       Summary: ( 1 ) Alignment number: the default value of the compiler and the smaller value of the size of the member

8 for                Visual environment and 4 for Linux .

             ( 2 ) The total size is an integer multiple of the maximum number of alignments in the member.

             ( 3 ) Except for the first one, all the preceding members are integer multiples of their own alignment numbers.

             ( 4 ) The alignment number of the structure is the maximum alignment number among the members.

3. Structure parameters

  struct student {...}s;

       Int main()

       { print (&s);// Pass the address, which will improve performance

}

bit segment

  1. The declaration of the bit field is similar to the structure.

   Struct k

{ int _a:2;// 2 bits,

  Int _b:5;// 5 bits,

  Int _c:10;// 10 bits

          / The first three use only 17 bits, and there are 15 free on 32 -bit platforms

  Int _d:30;// 30 bits, not enough, create a new one, so a total of

} ;           8 bytes, two integers

Note: members must be int, unsigned int and signed int , char integer family

2. The bit segment also needs to pay attention to memory alignment

 struct s

{

    char a : 3;// 3 bits

    char b : 4;// 4 bits, 7 in total, occupying one byte

    char c : 5; // 5 bits, one byte

    char d : 4; // 4 bits, one byte

    Int e : 4;// There are three in front, add one, add 4, a total of 8

};

enumerate

enum day// enumeration type

{

Mon=1,

Tue=2,

Wed=3,

Sun=7,

}c1;

 Enum day c1=sun;// Can only assign enumeration constants

Direct assignment is not possible : c1=7;

union (community)

Features of the Commonwealth: At least save the largest member

 union one

{

short c[7];// The size is 7, aligned to 12 which is an integer multiple of 4

int i;// Add 4 to 16

};

 

 

                

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521696&siteId=291194637