C language union and enumeration

Usually there will appear and the structure with union and enumerated types, these three same syntax, different context use only.

Union

Union can be different types of variables stored in the same memory section unit, which is the origin of the union.

grammar

union unionname
{
    datatype member1;
    datatype member2;
    datatype member3;
};

Definition and initialization

Look at the following program:

#include <stdio.h>

typedef union student1
{
    short num;
    char sex;
    int score;
}USTU;

typedef struct student2
{
    short num;
    char sex;
    int score;
}SSTU;

int main()
{
    USTU stu1 = {10,'x',80};
    SSTU stu2 = {20,'y',90};

    printf("sizeof(USTU) = %d\n",sizeof(USTU));
    printf("&stu1.num = %p,stu1.num = %d\n",&stu1.num,stu1.num);
    printf("&stu1.sex = %p,stu1.sex = %c\n",&stu1.sex,stu1.sex);
    printf("&stu1.score = %p,stu1.score = %d\n",&stu1.score,stu1.score);

    printf("sizeof(SSTU) = %d\n",sizeof(SSTU));
    printf("&stu2.num = %p,stu2.num = %d\n",&stu2.num,stu2.num);
    printf("&stu2.sex = %p,stu2.sex = %c\n",&stu2.sex,stu2.sex);
    printf("&stu2.score = %p,stu2.score = %d\n",&stu2.score,stu2.score);

    return 0;
}

The results are:

sizeof(USTU) = 4
&stu1.num = 0x7fffec657b40,stu1.num = 10
&stu1.sex = 0x7fffec657b40,stu1.sex = 

&stu1.score = 0x7fffec657b40,stu1.score = 10
sizeof(SSTU) = 8
&stu2.num = 0x7fffec657b50,stu2.num = 20
&stu2.sex = 0x7fffec657b52,stu2.sex = y
&stu2.score = 0x7fffec657b54,stu2.score = 90

From the above results point of view:

  • The type and structure of the common type of construction the same manner
  • The output of the normal type variable structure, also linear array member pointer
  • Byte length is a member of the type common in the maximum length of the byte
  • Union type of the reference pointer all point to the same address of the first address, which is the union of the variable
  • From the results of printing union member of view, all the members of a common type of variable is initialized error
  • But only the first member of the initialization can achieve correct results
  • It is also conceivable union type variable good idea to define and then assign it would be better
  • Members of the union type variable assignment only with the most recent first assignment

Big-endian and little-endian mode,

  • Big-endian mode: data is stored in low memory addresses high, high data is stored in memory in the lower address
  • Big-endian mode: data is stored in low memory lower address, the data stored in the memory highs in the upper address

Using a common type of computer which can determine the size of:

#include <stdio.h>

typedef union student1
{
    char top;
    int num;
}USTU;

int main()
{
    USTU stu;

    stu.num = 0x41424344;
    
    printf("stu.top = %c\n",stu.top);
    printf("stu.num = %x\n",stu.num);

    return 0;
}

The results are:

stu.top = D
stu.num = 41424344

From the results, the low memory address, that is stored in little-endian mode data lower.

enumerate

You can define a set of enumerated type integer constant, thereby enhancing the readability of the program. Enumerated type sometimes used in a switch, use as a label.

grammar

enum enumname
{
    memberlist
};

Definition and initialization

#include <stdio.h>

typedef enum week
{
    MON,TUE,WED,THU,FRI,SAT,SUN
}DAY;

int main()
{

    printf("%d,%d,%d,%d,%d,%d,%d\n",MON,TUE,WED,THU,FRI,SAT,SUN);

    DAY day = MON;

    printf("%d\n",day);

    return 0;
}

The results are:

0,1,2,3,4,5,6
0

As can be seen from the above results:

  • Enumerated type is a collection of elements are concentrated in some of the self-named variables, and these are variable integer
  • When enumeration type configuration, between the respective element separated by commas
  • Must pay attention to the last element of the enumeration side no semicolon
  • If you do not assign to an element in the construction of an enumerated type, it defaults to the ordered sequence 0,1,2,3 ...
  • If the assignment to an element in the construction of an enumerated type is considered to be assigned incrementally starting from the position of the number of columns
  • Enumerated type can be seen as an alternative to #define preprocessing directive
  • Top-defined enumeration type variable can be assigned either to an integer value, it can also be assigned enumeration element
Published 77 original articles · won praise 5 · Views 4847

Guess you like

Origin blog.csdn.net/SAKURASANN/article/details/104658689