Union and enumeration

  1. Union

    • Community structure and the like, but also a structure type of data structure, since it is the type of structure, we first define the other type, then the type defined variables

    • Definition of the type of common methods and the very similar structure, the union can be changed struct

      • Some algorithms have performed, the need to make several different types of variables stored in the memory cell to the same segment, each overlap between several variables

      • Several different variables such common structure occupies a region of memory, in the C language, referred to as "union" type structure, all members of the union address space occupies the same section

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

    • For example

      • typedef struct data{

      • short int i;

      • char ch;

      • float f;

      • }DATAL;

      • The DATA temp1; structure variable temp1 minimum duty 7 bytes (without regard to byte alignment)

      •  

      • typedef union data{

      • short int i;

      • char ch;

      • float f;

      • }DATA;

      • The DATA temp2; temp2 union 4 bytes, i.e. i, ch, f 4 bytes common

      • printf("%d\n",sizeof(temp2));

      • printf("%p\n",&(temp2));

      • printf("%p\n",&(temp2.i));

      • printf("%p\n",&(temp2.ch));

      • printf("%p\n",&(temp2.f));

      • Results: temp2 size is 4 bytes, the following several addresses are the same, demonstrating the various members share the same memory cell bodies comprise

    • Features common body

      • The same memory segment can be used to store several different types of members, but each instant only one act

      • Union variables play a role of a member is a member of the last store, the original deposit after a new member

      • Address Union variables and addresses of its members are of the same address

      • Variable initialization union

    • union data a = {123}; initialize a union of the first member

    • For example

      • typedef union data{

      • unsigned char a;

      • unsigned int b;

      • } DATA;

      • int main () {

      • DATA temp;

      • temp.b = 0xff ff ff ff;

      • printf("temp.b =%x\n",temp.b);

      • temp.a = 0x0d;

      • pritnf("temp.a = %x\n",temp.a);

      • pritnf("temp.b =%x\n",temp.b);

      • }

      • 结果 temp.b = ffffffff temp.a =d temp.b = ffffff0d

  2. enumerate

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

    • Enumerated type is also a type of structure, since it is constructed type of data types, you have to first define the type, and then define the variable

    • Enumerated type definition method

      • enum sheet 举类 type name {

      • Enumeration values ​​list:

      • };

      • It shall list all available values ​​enumeration value in the table, also called enumeration element

      • Enum take only the listed elements enumerated value

    • Define methods of enumeration

      • enum enumeration type enum name names;

    • For example

      • Defined enumerated type week

      • enum week // enum type

      • {

      • mon, Tue, Wed, you, Fri, sat, sun

      • };

      • enum week workday, weekday; // enum

      • workday and weekday can only take a sun .... sat in

      • workday = mon; // correct

      • workday = tue; // correct

      • workday = abc; // error, no enumeration value abc

    • Note: The enumeration value is constant. The program can no longer be re-assigned to it by assignment

      • The column sun = 5; mon = 2; sun = mon; is wrong

      • Enumeration element itself defines an array of sequence number represented by the system, the default is sequentially defined from 0 ... 0,1,2

        • As the week, mon is 0, tue value of 1, ...., sun value 6

      • It can be used to change the enumeration value of defaults:

        • enum week // enum type

        • {

        • mon = 3, Tue, Wed, you, Fri = 4, sat, sun

        • };

        • mon = 3 tue = 4, and so on

        • fri = 4 so

      • Note: The enumeration in the definition of the enumeration type when the elements can be assigned to it by an equal sign, to represent its value, in the program, not again enumerate the elements assignment as the enumeration element is constant

      •  

Guess you like

Origin www.cnblogs.com/fengzi759/p/11618715.html