Knowledge point 11: structure and union

One, the structure

1. Define the structure type: structure name and member list

	struct Date   //结构体名(大写以突出)   
	{
    
    
		int month;
		int day;
		int year;  //成员表列 
	};//最后要有一个分号!! 
	//建立了一个结构体类型之后,Date就成为了一个新的变量类型
	struct Student
	{
    
    
		char name[20];
		float score;
		struct Date birthday;	//成员可以属于另一个结构体类型,即结构体可以嵌套(定义在先) 
	};		 

Note: Variable type ≠ \not== Variable
2. The definition of structure variables

	struct Student s1,s2;//定义结构体类型的变量(可以先声明再定义)
	/*也可以在声明类型的同时定义变量:
	struct Student
	{
		char name[20];
		float score;
		struct Date birthday;
	}s1,s2; 
	*/
	/*若该结构体类型只用一次,也可以不命名,直接定义
	struct
	{
		char name[20];
		float score;
		struct Date birthday;
	}s1,s2;
	*/ 	

3. Initialization of structure variables The structure
can be initialized during or after definition. Under normal circumstances, initialization is performed at the same time as the definition, because that is more convenient. If you initialize after the definition, you can only assign values ​​to the members one by one (just like an array, an array can only initialize all elements at one time when it is defined. If it is initialized after the definition, it can only be one One is assigned), which is more troublesome.

	struct Student s1 = {
    
    "shao", 100, {
    
    5, 17, 2002}};//方便
	/*或:
	struct Student s1;
	strcpy(s1.name, "shao");//注意:字符串不能直接赋值,要用strcpy函数
	s1.score = 100;
	s1.birthday.month = 5;//需要逐级找到最低级的成员才能使用
	...  */

4. Structure array
Each element is a structure variable

struct employee
{
    
    
	int num;
	char name[20];
	float salary;
}emp[3] = {
    
    
	{
    
    001, "lemon", 2000},
	{
    
    002, "apple", 2300},
	{
    
    003, "peach", 2500}
	};

5. Structure variables are used as function parameters

#include <stdio.h>
struct Student
	{
    
    
		int num;
		float score[3];//结构体中存在数组成员 
		float aver;
	};
void input(struct Student stu[])
{
    
    
	int i;
	for (i = 0; i < 3; i ++)
	{
    
    
		scanf("%d %f %f %f", &stu[i].num, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);//数组成员的元素要一个一个单独输入	
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;//不用输入aver,因为该值是计算出来的 
	}
}
struct Student max(struct Student stu[])//函数的类型也要用结构体类型  
{
    
    										//函数运行的结果为一个结构体数组的元素
	int i, m = 0;
	for (i = 0; i < 3; i ++)
		if (stu[i].aver > stu[m].aver) 
			m = i;//用m来存放成绩最高的学生在数组中的序号 
	return (stu[m]);
}
void print(struct Student stud)//该函数的形参是一个变量而不是一个数组,直接将变量的信息传递过来而不是地址 
{
    
    
	printf("%d %lf %lf %lf", stud.num, stud.score[0], stud.score[1], stud.score[2]);
}

int main()
{
    
    
	struct Student stu[3];
	input(stu);
	print(max(stu));//简化主程序 
	return 0;
} 

Use structure variables in the position of the formal parameters, and during the function call, the formal parameters also occupy memory. Therefore, this transmission method is relatively expensive in space and time. In addition, "value transfer" makes that if the value of a variable is modified in a function, the change will not be returned to the main program.
6. The pointer of the structure variable is used as the function parameter

#include <stdio.h>
struct Student
	{
    
    
		int num;
		float score[3]; 
		float aver;
	};
void input(struct Student *p)
{
    
    
	int i;
	for (i = 0; i < 3; i ++, p ++)
	{
    
    
		scanf("%d %f %f %f", &(*p).num, &(*p).score[0], &(*p).score[1], &(*p).score[2]);	
		p->aver = (p->score[0] + p->score[1] + p->score[2]) / 3.0; 
	}
}
struct Student max(struct Student stu[])//函数的类型也要用结构体类型  
{
    
    										//函数运行的结果为一个结构体数组的元素
	int i, m = 0;
	for (i = 0; i < 3; i ++)
		if (stu[i].aver > stu[m].aver) 
			m = i;//用m来存放成绩最高的学生在数组中的序号 
	return (stu[m]);
}
void print(struct Student stud)//该函数的形参是一个变量而不是一个数组,直接将变量的信息传递过来而不是地址 
{
    
    
	printf("%d %lf %lf %lf", stud.num, stud.score[0], stud.score[1], stud.score[2]);
}

int main()
{
    
    
	struct Student stu[3], *p = stu;
	input(p);
	print(max(p));
	return 0;
} 

Second, the union

The definition of union variables is very similar to structure variables

union Data
{
    
    
	int i;
	char ch;
}a, b, c;

It is worth noting that the size of the structure variable is the total number of bytes occupied by the contained members, and each member has its own memory unit; the size of the union is the maximum memory length in the contained members.
The address of the union variable and the address of each member are the same address.
Although the members in the union share a section of memory, only members can be referenced when quoting, but the union variable cannot be directly referenced. printf("%d", a);it's wrong.
When initializing a union variable, only one initialization value is enough, and its type should be the same as the type of the first member of the union.
Only one member of the union has a role.

#include<stdio.h>
union Data
{
    
    
	int i;
	char ch;
}a;
int main()
{
    
    
	a.i = 97;//看按照什么形式来进行存储 
	printf("%d %c", a.i, a.ch);//根据内存单元中的信息来进行使用 
	union Data c = {
    
    1, 'a'};//初始化表中只能有一个常量,起作用的只是最后一次赋值的量,因为所有二进制位都会覆盖(取代) 
	printf("\n%d", a.i); //会输出97 
	return 0;
} 

Guess you like

Origin blog.csdn.net/Shao_yihao/article/details/113347487