# [C language] The structure struct that must be learned before learning the data structure is detailed

2022-11-24 22:25:35

1、结构体的声明与定义

1.1结构体是什么？

1.2Why should there be structure？

1.3结构体的声明

1.4结构体成员类型

1.5结构体变量定义和初始化

2、结构体成员的访问

3、结构体传参

# 1、结构体的声明与定义

## 1.2Why should there be structure？

We live a lot复杂对象,比如一个人、一本书.A man has a name,性别,年龄,工资,native place to describe.A book consists of its title、作者、出版社、price to describe.These descriptions are thus composed of complex objects that are structures.好了说了那么多,How to describe these complex objects in the form of code？请往下看.

## 1.3结构体的声明

``````struct stu
{
type member1;
type member2;
type member3;
...;
}variavle-list;``````

``````#include<stdio.h>

struct Person
{
//The following five are knot members
char name[10];
int age;
char sex[5];
float salary;
char place[10];
}s4,s5;//声明的同时定义s4,s5
int main()
{
struct Person s1, s2, s3;//s1-s3是结构体变量（局部的）
return 0;
}``````

mainabove the functionstruct Person以及{}The content inside is the declaration of the structure,Declares the type and size of the structure members.{}后面定义的s3,s4是全局的结构体变量.main函数里面定义的s1,s2,s3这三个是局部结构体变量.Only these five variables are created,Only five blocks of space will be opened up in memory.These five structure variables store the five structure members in the structure declaration just now.

## 1.4结构体成员类型

Then the structure is a collection of arrays of different types,结构体的成员可以是变量、数组、指针、Or even chess structures.Below we use code to explain.

## 1.5结构体变量定义和初始化

1.3中我们定了5个结构体变量,We see that there are three ways of defining.These definitions all have structure types,can be defined.There are actually three ways of defining it：

• 声明类型的同时,在;The global structure variables defined earlier
• 单独定义结构体变量
• mainDefine local structure variables in the function

``````#include<stdio.h>

struct Person
{
//The following five are knot members
char name[10];
int age;
char sex[5];
float salary;
char place[10];
}s4,s5;//声明的同时定义s4,s5

struct Person s6;

int main()
{
struct Person s1, s2, s3;//s1-s3是结构体变量（局部的）
return 0;
}``````

s6Belongs to a structure variable defined separately;s1-s2属于在main函数里面定义局部的结构体变量.

I believe everyone has understood the definition of structure variables,Let's look at the initialization of structure variables.

``````#include<stdio.h>

struct Person
{
char name[10];
int age;
char sex[5];
float salary;
char place[10];
};

int main()
{
struct Person s1 = { "张三",22,"男",12.3f,"美国" };
struct Person s2 = { "李四",23,"女",66.2f,"荷兰" };
struct Person s3 = { "王五",66,"保密",0.2f,"布吉岛" };
printf("%s %d %s %.1f %s\n", s1.name, s1.age, s1.sex, s1.salary, s1.place);
printf("%s %d %s %.1f %s\n", s2.name, s2.age, s2.sex, s2.salary, s2.place);
printf("%s %d %s %.1f %s\n", s3.name, s3.age, s3.sex, s3.salary, s3.place);
return 0;
}``````

When we want to access structure members,We use struct variables Number structure members to obtain.注意,You can write whatever type of format specifier you initialize, for example"张三"The corresponding format specifier is %s,I believe you already know how to initialize structure variables.

• Struct member types can be of different types,普通数据类型,数组,指针,甚至是结构体
• When the structure variable is initialized,Corresponding to the structure member type to initialize
• Struct variables are used when accessing structure members.操作符来访问
• When outputting each structure member, it should correspond to the format specifier of the type

Finally, let's see what it looks like when the struct members are structs：

``````#include<stdio.h>

struct S1
{
int a;
char b;
};
struct S2
{
float c;
struct S1 f;
double d;
};
int main()
{
struct S2 s = { 2.1f,{10,'A'},3.4 };
printf("%.1f %d %c %.1lf\n", s.c, s.f.a, s.f.b, s.d);
return 0;
}``````

输出结果：2.1 10 A 3.4

structS2The structure type is defined as structure in ,Some are like nesting dolls.Let me explain how to use them：

The structure type is struct的结构体成员Initialization only needs to be done at time{}里面再加一个{}就好了,{}Inside, the member can be initialized as a structure member in the structure.

Structure variables are accessed as structure membersA struct member of a structThe structure members inside need to use two.号 .As above procedure,Two dots to get.A structure type is a member of a structure.

# 2、结构体成员的访问

The access to structure members believes that everyone has learned a method in the above explanation, then pass it.号来访问,Another way is through->来访问.The two access methods are ：

• .操作符
• ->符号
``````#include<stdio.h>

struct Person
{
char name[10];
int age;
char sex[5];
};

void Print1(struct Person* p)
{
printf("%s %d %s\n", (*p).name, (*p).age, (*p).sex);
printf("%s %d %s\n", p->name, p->age, p->sex);
}

int main()
{
struct Person s = { "张三",60,"男" };
Print1(&s);
return 0;
}``````

The above code is to put the structure variablesThe address of is passed to the pointer structure variablep.Then we can pass dereferencepRevisit the structure members,访问方式就是.号.->If the number permission is relatively large, you can directly access the member without dereferencing.

# 3、结构体传参

When we learn about arrays,There are two methods of passing parameters for array parameters: passing by value and passing parameters.The same goes for structs,Both parameters and addresses can be passed.有以下代码：

``````#include<stdio.h>

struct Person
{
char name[10];
int age;
char sex[5];
};

void Print1(struct Person p1)
{
printf("%s %d %s\n", p1.name, p1.age, p1.sex);
}

void Print2(struct Person* p2)
{
printf("%s %d %s\n", (*p2).name, (*p2).age, (*p2).sex);
printf("%s %d %s\n", p2->name, p2->age, p2->sex);
}

int main()
{
struct Person s = { "张三",60,"男" };
Print1(s);
Print2(&s);
return 0;
}``````

Print1What is passed in the formal parameter is the value,此时p1等同于s.依次通过.No. to access member variables just fine.

Print2What is passed in the formal parameter is the address,此时p1的地址等同于s的地址,我们需要先对p1Dereference is performed to access member variables in turn.我们也可以用->直接访问成员变量.

Never Give Up