Union/union, enumeration, typedef

Table of contents

1. Introduction of the concept of consortium and community

1.1 Define a union:

1.2 Define a union variable:

1.3 Unions are similar to structures, but there are differences:

2. The community should pay attention to data coverage issues

3. Community Development Cases

4. Introduction to enumeration types

4.1 What is an enumeration type:

4.2 How to define an enumeration type:

4.3 Define an enumeration type variable:

        4.3.1 Method 1 for defining enumeration type variables:

        4.3.2 Method 2 for defining enumeration type variables:

4.4 Enumeration variables store the values ​​of enumeration constants:

        4.4.1 Limited to several cases of enumeration lists:

        4.4.2 The value of the enumeration list starts from 0 by default:

        4.4.3 You can specify the value of the number in the enumeration list:

        4.4.4 You can directly ignore the name after enum and define enumeration variables:

5. typedef keyword

5.1 Introduction to typedef keyword:

5.2 Typedef keyword and structure are used together:

        5.2.1 Combination of typedef and structure 1:

        5.2.2 Combination of typedef and structure 2:

5.3 Typedef and structure cases:


1. Introduction of the concept of consortium and community

  • Sometimes the same memory space stores different types, and variables of different types share a space.

  • Structure elements have their own separate space

  • Union elements share space, and the size of the space is determined by the largest type.

1.1 Define a union:

union Test
{
    int idata;
    char cdata;
    double ddata;
};

1.2 Define a union variable:

#include <stdio.h>

union Test
{
    int idata;
    char cdata;
    double ddata;
};

int main()
{
    union Test u1;	//定义一个联合体变量
    return 0;
}

1.3 Unions are similar to structures, but there are differences:

#include <stdio.h>

struct TestT	//定义一个结构体
{
	int idata;
    char cdata;
    double ddata;
};

union TestU		//定义一个联合体
{
    int idata;
    char cdata;
    double ddata;
};
    

int main()
{
    struct TestT t1;	//定义一个结构体变量
    union  TestU u1;	//定义一个联合体变量
    
    printf("结构体t1的大小是:%d\n",sizeof(t1));	//计算结构体t1的大小
    printf("联合体u1的大小是:%d\n",sizeof(u1));	//计算联合体u1的大小
    
    //查看结构体没一个成员变量的内存地址
    printf("idata = %p\n",&t1.idata);				
    printf("cdata = %p\n",&t1.cdata);
    printf("ddata = %p\n",&t1.ddata);
    
    //查看联合体每一个成员变量的内存地址
    printf("idata = %p\n",&u1.idata);
    printf("cdata = %p\n",&u1.cdata);
    printf("ddata = %p\n",&u1.ddata);
    return 0;
}
/*程序运行结果:
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_union.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
结构体t1的大小是:16
联合体u1的大小是:8
idata = 000000000061FE10
cdata = 000000000061FE14
ddata = 000000000061FE18
idata = 000000000061FE08
cdata = 000000000061FE08
ddata = 000000000061FE08
*/
  • Although unions and structures look similar, they are different. There are differences.

  • Structure size:

  • Due to the problem of storage variable address alignment, structure size calculation must meet two principles:

    1. The offset of a structure member must be an integer multiple of the member size.

    2. The size of the structure must be an integer multiple of the size of all members.

  • All members of the union refer to the same location in memory, and the memory length of the largest member is used as the memory size of the union.

  • Although a union can have multiple members, only one of them can be stored at the same time.

2. The community should pay attention to data coverage issues

  • Struct elements do not affect each other, and assignment to a union will lead to overwriting.

#include <stdio.h>

struct TestT
{
	int idata;
	char cdata;
	double ddata;
};

union TestU
{
	int idata;
	char cdata;
	int ddata;
};

int main()
{
	struct TestT t1;
	union  TestU u1;
	
	printf("结构体t1的大小是:%d\n",sizeof(t1));
	printf("联合体u1的大小是:%d\n",sizeof(u1));
	
	t1.idata = 10;
	t1.cdata = 'c';
	t1.ddata = 2.3;
	printf("idata = %p,%d\n",&t1.idata,t1.idata);
	printf("cdata = %p,%c\n",&t1.cdata,t1.cdata);
	printf("ddata = %p,%.2lf\n",&t1.ddata,t1.ddata);
	
	u1.idata = 10;
	u1.cdata = 'c';
	u1.ddata = 20;
	printf("idata = %d\n",u1.idata);	//联合体数据会被覆盖
	printf("idata = %p\n",&u1.idata);
	printf("cdata = %p\n",&u1.cdata);
	printf("ddata = %p\n",&u1.ddata);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_union.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
结构体t1的大小是:16
联合体u1的大小是:4
idata = 000000000061FE10,10
cdata = 000000000061FE14,c
ddata = 000000000061FE18,2.30
idata = 20
idata = 000000000061FE0C
cdata = 000000000061FE0C
ddata = 000000000061FE0C
*/

3. Community Development Cases

Example: There is data for several people, including students and teachers. The student data includes: age, name, number, gender, occupation, and class. The teacher data includes: age, name, number, gender, occupation, and subject. It is required to use the same table to process:

#include <stdio.h>

struct Person
{
	int age;
	char name[32];
	char telephone[32];
	char sex[12];
	char zhiYe;
	union{
		int class;
		char keMu[12];
	}mes;
};

int main()
{
	struct Person p[2];
	int i;
	
	for(i=0; i<2; i++){
		printf("情输入职业:t代表老师,s代表学生\n");
		scanf("%c",&p[i].zhiYe);
		if(p[i].zhiYe == 's'){
			printf("请输入学生的年龄:\n");
			scanf("%d",&p[i].age);
			printf("请输入学生的名字:\n");
			scanf("%s",&p[i].name);
			printf("请输入学生的电话:\n");
			scanf("%s",&p[i].telephone);
			printf("请输入学生的性别:\n");
			scanf("%s",&p[i].sex);
			printf("请输入学生的班级:\n");
			scanf("%d",&(p[i].mes.class));
		}else{
			printf("请输入老师的年龄:\n");
			scanf("%d",&p[i].age);
			printf("请输入老师的名字:\n");
			scanf("%s",&p[i].name);
			printf("请输入老师的电话:\n");
			scanf("%s",&p[i].telephone);
			printf("请输入老师的性别:\n");
			scanf("%s",&p[i].sex);
			printf("请输入老师的科目:\n");
			scanf("%s",&(p[i].mes.keMu));
		}
		getchar();
	}
	for(i=0; i<2; i++){
		if(p[i].zhiYe == 's'){
			printf("学生的信息是:\n");
			printf("年龄:%d,名字:%s,电话:%s,性别:%s,班级:%d\n",	\
					p[i].age,p[i].name,p[i].telephone,p[i].sex,p[i].mes.class);
		}else{
			printf("老师的信息是:\n");
			printf("年龄:%d,名字:%s,电话:%s,性别:%s,科目:%s\n",	\
					p[i].age,p[i].name,p[i].telephone,p[i].sex,p[i].mes.keMu);
		}
	}
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_union_ex.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
情输入职业:t代表老师,s代表学生
s
请输入学生的年龄:
21
请输入学生的名字:
shiyaho
请输入学生的电话:
19563523860
请输入学生的性别:
请输入学生的班级:
4
情输入职业:t代表老师,s代表学生
t
请输入老师的年龄:
35
请输入老师的名字:
chenlichen.
请输入老师的电话:
666666666666
请输入老师的性别:
请输入老师的科目:
linux
学生的信息是:
年龄:21,名字:shiyaho,电话:19563523860,性别:男,班级:4
老师的信息是:
年龄:35,名字:chenlichen.,电话:666666666666,性别:男,科目:linux
*/

4. Introduction to enumeration types

4.1 What is an enumeration type:

  • If a variable has only a few possible values, such as the day of the week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

4.2 How to define an enumeration type:

enum WeekDay {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};
  • The names in the list can be defined by yourself, there is no need to apply for them like variables

  • The C compiler treats it as a constant , also called an enumeration constant.

4.3 Define an enumeration type variable:

4.3.1 Method 1 for defining enumeration type variables:

#include <stdio.h>

enum WeekDay {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};

int main()
{
	enum WeekDay w;	//定义一个枚举类型变量
	return 0;
}

4.3.2 Method 2 for defining enumeration type variables:

#include <stdio.h>

enum WeekDay {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}w1,w2;

int main()
{

	return 0;
}

4.4 Enumeration variables store the values ​​of enumeration constants:

4.4.1 Limited to several cases of enumeration lists:

/*
	只限于枚举列表当中的几种情况,其他情况不行
*/
#include <stdio.h>

enum WeekDay {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};

int main()
{
	enum WeekDay w1;
	
	w = ttt;
	printf("w = %d\n",w);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_enum.c
demo_enum.c: In function 'main':
demo_enum.c:9:6: error: 'ttt' undeclared (first use in this function)
  w = ttt;
      ^~~
demo_enum.c:9:6: note: each undeclared identifier is reported only once for each function it appears in
*/

4.4.2 The value of the enumeration list starts from 0 by default:

#include <stdio.h>

enum WeekDay {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};

int main()
{
	enum WeekDay w;
	
	w = Monday;
	printf("w = %d\n",w);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_enum.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
w = 0
*/
  • The value of the enumeration list starts from 0 by default. Although it looks like a variable name, it cannot be assigned a value.

4.4.3 You can specify the value of the number in the enumeration list:

#include <stdio.h>

enum WeekDay {Monday,Tuesday,Wednesday = 8,Thursday,Friday,Saturday,Sunday};

int main()
{
	enum WeekDay w;
	
	w = Wednesday;
	printf("w = %d\n",w);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_enum.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
w = 8
*/

4.4.4 You can directly ignore the name after enum and define enumeration variables:

#include <stdio.h>

enum WeekDay {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}w1,w2;

int main()
{
	
	w1 = Monday;
	w2 = Tuesday;
	printf("w1 = %d\n",w1);
	printf("w2 = %d\n",w2);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_enum.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
w1 = 0
w2 = 1
*/

5. typedef keyword

5.1 Introduction to typedef keyword:

  • The role of the typedef keyword: give a new name to an existing variable type

#include <stdio.h>

typedef char ziFu;
typedef int zhengShu;
typedef double xiaoShu;

int main()
{
	ziFu cdata = 'c';
	printf("cdata = %c\n",cdata);
	
	zhengShu idata = 10;
	printf("idata = %d\n",idata);
	
	xiaoShu fdata = 2.3;
	printf("fdata = %.2lf\n",fdata);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_typedef.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
cdata = c
idata = 10
fdata = 2.30
*/
  • Let’s just give an example. If you use this method, you won’t be afraid of being beaten to death by your supervisor, haha.

  • Commonly used practices in microcontrollers

/*在单片机中经常这样使用*/
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;

5.2 Typedef keyword and structure are used together:

  • Typedef is generally used with structures, which is also a convenience. Don’t start with struct every time.

5.2.1 Combination of typedef and structure 1:

#include <stdio.h>

struct Test
{
	int idata;
	char cdata;
};

typedef struct Test T;

int main()
{
	T t1;	//定义一个结构体变量
	
	t1.idata = 10;
	t1.cdata = '#';
	printf("idata = %d,cdata = %c\n",t1.idata,t1.cdata);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_typedef.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
idata = 10,cdata = #
*/

5.2.2 Combination of typedef and structure 2:

#include <stdio.h>

typedef struct
{
	int idata;
	char cdata;
}Demo;

int main()
{
	Demo d;
	
	d.idata = 10;
	d.cdata = '#';
	printf("idata = %d,cdata = %c\n",d.idata,d.cdata);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_typedef.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
idata = 10,cdata = #
*/

5.3 Typedef and structure cases:

#include <stdio.h>

typedef struct 
{
	int num;
	int age;
	char name[32];
	char sex[12];
	char addr[32];
}Person,*pPerson;

void printInof(Person p)
{
	printf("%d号姐姐是:年龄:%d,名字:%s,性别:%s,地址:%s\n",p.num,p.age,p.name,p.sex,p.addr);
}

void printInof2(pPerson pp)
{
	printf("%d号姐姐是:年龄:%d,名字:%s,性别:%s,地址:%s\n",pp->num,pp->age,pp->name,pp->sex,pp->addr);
}

void printInof3(Person *p)
{
	printf("%d号姐姐是:年龄:%d,名字:%s,性别:%s,地址:%s\n",p->num,p->age,p->name,p->sex,p->addr);
}

int main()
{
	Person m1 = {1,19,"莉莉","女","重庆"};
	Person m2 = {2,20,"美美","女","成都"};
	Person m3 = {3,22,"小露","女","永州"};
	
	printInof(m1);
	printInof(m2);
	printInof(m3);
	
	pPerson pp1 = &m1;
	pPerson pp2 = &m2;
	pPerson pp3 = &m3;
	
	printInof2(pp1);
	printInof2(pp2);
	printInof2(pp3);
	
	Person *p1 = &m1;
	Person *p2 = &m2;
	Person *p3 = &m3;
	
	printInof3(p1);
	printInof3(p2);
	printInof3(p3);
	return 0;
}
/*
E:\code\一阶段C语言\第九章_联合体共用体>gcc demo_typedef_struct_ex.c

E:\code\一阶段C语言\第九章_联合体共用体>a.exe
1号姐姐是:年龄:19,名字:莉莉,性别:女,地址:重庆
2号姐姐是:年龄:20,名字:美美,性别:女,地址:成都
3号姐姐是:年龄:22,名字:小露,性别:女,地址:永州
1号姐姐是:年龄:19,名字:莉莉,性别:女,地址:重庆
2号姐姐是:年龄:20,名字:美美,性别:女,地址:成都
3号姐姐是:年龄:22,名字:小露,性别:女,地址:永州
1号姐姐是:年龄:19,名字:莉莉,性别:女,地址:重庆
2号姐姐是:年龄:20,名字:美美,性别:女,地址:成都
3号姐姐是:年龄:22,名字:小露,性别:女,地址:永州
*/

Guess you like

Origin blog.csdn.net/weixin_54859557/article/details/129353238