Common body and enumeration in C language

There are also common body and enumeration types that appear together with the structure. The syntax format of these three types is the same, but they are only used in different situations.

Shared body

The common body can store different types of variables in the same memory unit, which is the origin of union.

grammar

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

Definition and initialization

First look at the following procedure:

#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 result is:

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:

  • Common body type and structural body type are constructed in the same way
  • The output of variable of constructor type is normal, and the member pointers are also linear
  • The byte length of the common body type is the one with the largest byte length among members
  • The member pointers of a common type variable all point to the same address, that is, the first address of the common type variable
  • From the result of printing common body members, it is wrong to initialize all common body type variable members
  • But only initializing the first member can achieve the correct result
  • It can also be thought that it would be better to define and assign the common type variable first
  • The member assignment of a common type variable is only related to the latest assignment

Large end mode and small end mode

  • Large end mode: the data low position is saved in the memory high address, and the data high position is saved in the memory low address
  • Large end mode: the low data bits are stored in the low memory address, and the high data bits are stored in the high memory address

The size end of the computer can be determined by the type of the common body:

#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 result is:

stu.top = D
stu.num = 41424344

It can be seen from the results that the low bit of data is stored in the low bit address of memory, which is the small end mode.

enumeration

Enumeration type can define a set of integer constants to improve the readability of the program. Enumeration types are sometimes used in switch es 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 result is:

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

From the above results, we can see that:

  • Enumeration type is a collection. The elements in the collection are all self named variables, and the variables are all integer
  • When constructing enumeration types, the elements are separated by commas
  • Be sure to note that there is no semicolon at the end of the enumeration element
  • If an element is not assigned a value when constructing an enumeration type, it defaults to an ordered sequence of 0, 1, 2, 3
  • If an element is assigned a value when constructing an enumeration type, it is considered to be an increasing sequence starting from the assigned position
  • Enumeration type can be regarded as an alternative to preprocessing instruction ා define
  • The enumeration type variable defined above can be assigned to either an integer value or an enumeration element
77 original articles published, praised 5, 4835 visitors
Private letter follow

Posted by hmogan on Wed, 04 Mar 2020 06:47:58 -0800