[Elementary C Language] Learn Structure

1. Declaration of structure type
2. Structure initialization
3. Structure member access
4. Structure parameter passing
 


Preface: A structure is a collection of values ​​called member variables. Each member of the structure can be a variable of a different type.

1. Declaration of structure type

1. Declaration of structure

Template for structure declaration:

struct tag
{
   member-list;
}variable-list;

   This is a template declaration, and the resulting structure is just one type, the same as int char.

For example, we need to describe the information of a student, can we use int type to describe the student? Obviously not, because students include student number (char), name (char), age (int) and gender (char), so this is not a single type, and a structure is needed to describe the student.

The first declaration method: the full name of this structure is: strcut Stu

struct Stu
{
    char name[20];//名字
    int age;//年龄
    char sex[5];//性别
    char id[20];//学号
};//这里的分号不能丢

The second way: rename

typedef struct Stu
{
char name[20];//名字
int age;//年龄
char sex[5];//性别
char id[20];//学号
}Stu;//给结构体类型重新取名为Stu

You can also rename it like this:

struct Stu
{
char name[20];//名字
int age;//年龄
char sex[5];//性别
char id[20];//学号
};
typedef struct Stu Stu;//重新起一行来命名

The third way: create variables at the time of declaration

struct Stu
{
    char name[20];//名字
    int age;//年龄
    char sex[5];//性别
    char id[20];//学号
}S1;//这里的S1是结构体创造出来的全局变量

 The above series of operations are just declaring a structure type, and more content will follow.

2. Types of structure members

Preface: From the template above, we can know that member_list is a structure member, so what are the types of structure members?

Members of structures can be scalars, arrays, pointers, or even other structures.

struct A 
{
	char c;
	int a;
}; ​
struct B
{
	char b;
	int arr[10];
	struct A c;//成员为其他结构体成员
	struct A* F;//为指针类型
};

However, it should be noted that a structure member cannot be its own structure, but it can be a pointer to its own structure.

3. Creation and initialization of structure variables

In the previous time, we have introduced the global variables created at the time of declaration, and we will introduce them together next.

The first creation method:

#include<stdio.h>
struct Stu
{
	char c;
	int arr[10];
};
int main()
{
	struct Stu A;//结构体变量A
	struct Stu B;//结构体变量B
	return 0;
}

The variables A and B created here are both local variables.

The second creation method:

#include<stdio.h>
typedef struct Stu
{
	char c;
	int arr[10];
}Stu;//对结构体重命名
int main()
{
	//struct Stu A;//结构体变量A
	//struct Stu B;//结构体变量B
	Stu C;//结构体变量C
	return 0;
}

The third way: create global variables mentioned above

#include<stdio.h>
struct Stu
{
	char c;
	int arr[10];
}D;//全局变量D
int main()
{
	//struct Stu A;//结构体变量A
	//struct Stu B;//结构体变量B
	//Stu C;//结构体变量C
	return 0;
}

After the variable is created, it's time to initialize the variable

initialization:

#include<stdio.h>
struct Stu
{
	char name[20];
	int age;
	double height;
};
int main()
{
	struct Stu s1 = {"zhangsan",20,182.8};//顺序初始化
	struct Stu s2 = {.age=18,.height=188.5};//指定成员初始化
	return 0;
}

Initialize variables when they are created:

struct Stu
{
	char name[20];
	int age;
	double height;
}s3 = {"lisi",19,150.6};//创造的全局变量并初始化

2. Access to structure members

When the declaration of the structure, the creation and initialization of the structure variables are completed, the structure can be used, which is the knowledge related to the access of the structure members.

There are two operators for structure access: '.' operator and '->' operator

Next, we illustrate an example of structure member access through two operations of initialization and printing.

#include<stdio.h>
struct Stu
{
	double height;
	int age;
};
void set_s1(struct Stu* ps)//用结构体类型指针接收
{
	ps->age = 22;
	ps->height = 188.45;//用"->"操作符访问结构体成员
}
void print(struct Stu s)
{
	printf("%d %lf",s.age,s.height);
}
int main()
{
	struct Stu s1 = { 0 };//这里先初始化成0
	//用一个函数对结构体变量赋值
	set_s1(&s1);//因为需要修改值,所以必须要传址
	//用一个函数打印结构体变量中的数据
	print(s1);//只需要打印,不需要修改。传值就可以
	return 0;
}

Let's take a look at the results of the operation:

 In the above example, there are two operators, let's summarize when to use which one. (. and ->)

Structure variable. Member name Structure pointer -> member name

That is, when using pointers, the left side must be ->, and the other is the same.

From the above example, we can know that the structure can pass both values ​​and addresses. Next, let's take a look at the differences between the two.

1. pass parameters

If the parameters are passed, the space that needs to be re-opened will be very large, and in some cases the value passing cannot meet the requirements, because the formal parameters are a copy of the actual parameters

2. Addressing

Addressing can be applied to all situations, and the memory that needs to be re-opened is not large, so the method of structure addressing should be used as much as possible.

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/132057122