Do you really understand structures and unions?

content

structure

1. Why do you need a struct

2. Define the structure

3. Initialization of structure variables

4. Output of structure variables

5. Input of structure variables

6. Assignment between structure variables

 7. Array of Structures

8. Structure pointer

9. Structure pointer as function parameter

Union type

1. Definition of a Commonwealth

 2. Common body memory

3. Initialization of union variables

Epilogue


1. Why do you need a struct

In our daily life, we often encounter many complicated things. For example, a class needs to count the midterm test scores. The score table contains the name, class, student number, scores of each subject and total score of each student.

Name class student ID language English math total score
LiHua 1 123 100 90 80 270
LiMing 1 124 90 90 80 260

Each student corresponds to the corresponding class, student number, and grade. If a student's total score changes, his rank in the class will change. At this time, the data of the changed row must be moved to a new row together. We found that if it seems difficult to implement this kind of operation with the knowledge we have learned before, then we need to use the structure at this time .

2. Define the structure

※※Be careful to add a ";" after {}, it is easy for beginners to miss writing! ! !

The declaration of the structure:

struct student
{
	char name[20];
	int ID[10];
	int sorce;
};

※※Note: Memory is not allocated when declaring, and memory needs to be allocated when defining.

 It is better to define the structure type first, and then define the variable.

struct student
{
	char name[20];
	int ID[10];
	int sorce;
};//定义结构体类型
struct student stu1;//定义变量

3. Initialization of structure variables

Correct operation:

#include<stdio.h>

struct student
{
	char name[20];
	char ID[10];
	int sorce;
};

int main()
{
	struct student stu1={"LiHua","123",100};
	printf("%s %s %d",stu1.name,stu1.ID,stu1.sorce);
	return 0;
 } 

Wrong action:

#include<stdio.h>

struct student
{
	char name[20];
	char ID[10];
	int sorce;
};

int main()
{
	struct student stu1;
	stu1={"LiHua","123",100};
	printf("%s %s %d",stu1.name,stu1.ID,stu1.sorce);
	return 0;
 } 

Can you see the difference between the two codes above?

Yes, there is only one difference

//正确
struct student stu1={"LiHua","123",100};
//错误
struct student stu1;
stu1={"LiHua","123",100};

Don't make this mistake when initializing structs.

4. Output of structure variables

The structure cannot be output as a whole. To output which one, you must clearly specify the name of the member variable.

printf("%s %s %d",stu1.name,stu1.ID,stu1.sorce);

For ordinary variables, refer to member variables through the " . " operator.

"." is known as the member operator.

5. Input of structure variables

The input of the structure variable is similar to the output of the structure variable. It cannot be input as a whole. The address of the member variable must be clearly specified to which one is to be input.

struct student stu2;
scanf("%s %s %d",stu2.name,stu2.ID,&stu2.sorce);

6. Assignment between structure variables

Structure variables can be assigned directly

struct student stu1={"LiHua","123",100};
struct student stu3;
stu3=stu1;

printf("%s %s %d",stu3.name,stu3.ID,stu3.sorce);

 7. Array of Structures

int main()
{
	struct student stu[3]={
		{"LiHua","123",100},
		{"LiMing","124",90},
		{"LiLei","125",80}
	};
	for(int i=0;i<3;i++){
		printf("%s %s %d\n",stu[i].name,stu[i].ID,stu[i].sorce);
	}
	return 0;
 } 

8. Structure pointer

int main()
{
	struct student stu={"LiHua","123",100};
	struct student *p=0;
	p=&stu;
	printf("%s %s %d\n",stu.name,stu.ID,stu.sorce);
	printf("%s %s %d\n",(*p).name,(*p).ID,(*p).sorce);
	printf("%s %s %d\n",p->name,p->ID,p->sorce);
	return 0;
 } 

 

 There are two forms of referencing member variables by pointers:

(1) (*p).sorce=100;//*p is equivalent to stu

(2)p->sorce=100;

Pointer variables refer to member variables through the " -> " operator.

" -> " is also known as: the member operator.

Usually we are used to the second usage.

9. Structure pointer as function parameter

#include<stdio.h>

struct student
{
	char name[20];
	char ID[10];
	int sorce;
};
void STU(struct student *stu){
	stu->sorce=90;
}

int main()
{
	struct student stu={"LiHua","123",100};
	STU(&stu);
	printf("%s %s %d\n",stu.name,stu.ID,stu.sorce);
	return 0;
 } 

After passing the address of the structure stu, the value of stu.sorce successfully changed.

Union type

1. Definition of a Commonwealth

Unions are very similar to structs and have a lot in common in definition.

Union type definition:

union union name

{

        datatype member1;

        data type member 2;  

        data type member 3;

        .  .  .  .  .  .

}; 

There are differences in storage between a union and a structure. All members of the union share the same storage space (the space that occupies the largest storage space among all members).

Union variable definition:

When defining a union variable, it is best to define the union type first, and then define the union variable, the same as when defining a structure variable.

union data
{
	int a;
	char ch;
	float c;
};
union data UD; 

 2. Common body memory

3. Initialization of union variables

 When a union variable is defined, only the type value of one of its members can be initialized.

 Too much initialization gives the above error.

#include<stdio.h>

union data
{
	int a;
	char ch;
	float f;
};

int main()
{
	union data UD={10};
	printf("%d\n",UD.a);
	
	return 0;
 } 

Just initialize one and it works fine.

Epilogue

        Freshmen and non-undergraduate students hope to improve their programming skills through their own efforts, and hope to get everyone's support and attention. If there are any mistakes or inappropriate places in the article, I hope you can make suggestions, and I will definitely correct them. Thank you for watching! ! !

 

Guess you like

Origin blog.csdn.net/qq_61139806/article/details/124172529