Play C language with zero foundation - structure [beginner]

Hello everyone, I am deep fish~

Table of contents

[Preface]:

1. The declaration of the structure

1.1 Basic knowledge of structure

1.2 Declaration of structure

1.3 Types of structure members

1.4 Definition and initialization of structure variables

2. Access to structure members


[Preface]: This chapter introduces part of the knowledge of the structure, and will not explain it in depth. It is only the elementary part. I can understand the logic of the structure and use it easily. The advanced part will be explained in depth later. I hope it will be helpful to everyone. helpful

1. The declaration of the structure

1.1 Basic knowledge of structure

A structure is a collection of values ​​called member variables , and each member of a structure can be a variable of a different type

This reminds us of an array: a collection of elements of the same type

1.2 Declaration of structure

struct tag              // struct keyword + name (named according to the actual situation)

{

member-list; //List of member variables (one or more)

}variable-list // variable list

Why is there a structure?

The types we have learned so far are all built-in types, eg: char, int, short, double... But if we want to use a type to create a complex object , such as a person: continue to ask for name + gender + age + phone + address +... At this time, C language introduced the type of structure to create complex objects (for a variable, we must first have a type to create this variable, and then store the value in the variable)

 Suppose we want to create variables for students:

Student Type --> Student Variables --> Student Attribute Information

Code:

#include<stdio.h>
struct Stu
{
	//学生的相关的属性
	char name[20];
	int age;
	char sex[5];
	char tele[12];
}s3,s4;           //这里的s3,s4是变量名
struct Stu s5;     //这样创建全局变量也是可以的
int main()
{
	struct Stu s1;  //注意这里类型是struct Stu(struct是不可以省略的)
	struct Stu s2;
	return 0;
}

The above one creates five variables s1, s2, s3, s4, s5, but s1, s2 are local variables , s3, s4, s5 are global variables

If we want to simplify the type of the variable : rename

typedef struct Stu
{
	//学生的相关的属性
	char name[20];
	int age;
	char sex[5];
	char tele[12];
}Stu;    //Stu是重命名产生的新的类型
int main()
{
	Stu s5;
	return 0;
}

typedef is a type redefinition, which is to rename the name of the type

[Note] In the previous code, s3 and s4 are variable names , and in this code, Stu is a new type generated by renaming

1.3 Types of structure members

Structure members can be scalars, arrays, pointers, or even other structures

Code demonstration: We can see that the members of the structure are relatively rich

struct B
{
	char c;
	int i;
};
struct S
{
	char c;
	int num;
	int arr[10];
	double* pd;
	struct B sb;
	struct B* pb;
};
int main()
{
	return 0;
}

1.4 Definition and initialization of structure variables

definition:

There are two ways to create global variables:

(1) Add the variable name directly before the semicolon in the structure declaration;

(2) After the structure declaration ends, it is the same as local variable creation, but it is not written in the main function

initialization:

(1) Initialize in order: use { } like an array, and initialize according to the order of members in the middle

(2) Specify member initialization : In this way, the name of the member must be added eg: .num =1000 , and other uninitialized parts default to 0

The code demonstration is as follows:

struct B
{
	char c;
	int i;
};
struct S
{
	char c;
	int num;
	int arr[10];
	double* pd;
	struct B sb;
	struct B* pb;
}s1;  //s1是全局变量(第一种)

struct S s2;//s2是全局变量(第二种)

int main()
{
	double d = 3.14;
    //按照顺序初始化
	struct S s3 = { 'q',100,{1,2,3},&d,{'a',1},NULL };//局部变量
    //指定成员来初始化
    struct S s4={.num=1000,.arr={1,2,3,4,5}};
	return 0;
}

2. Access to structure members

·Structure variable.Member name

·Structure pointer -> member name

 Code demonstration: [write a function to store data in s]

#include<string.h>
struct S
{
	char name[20];
	int age;
};
void set_s(struct S* ps) 
{
	(*ps).age = 18;
	strcpy((*ps).name, "zhangsan");//字符串拷贝
	//(*ps).name = "zhangsan";这是错误的,因为name是数组名,数组名是地址常量
}
int main()
{
	struct S s = { 0 };
	//写一个函数给s种存放数据
	set_s(&s);

	return 0;
}

There are two points to note here :

(1) When storing data in name (array member), we cannot store it directly like age :

Because name is an array name, and the array name is an address constant, it is equivalent to putting Zhang San on a house number, but not in the room, so we use the strcpy function to store data

(2) When passing parameters, you cannot pass them directly, but pass in the address of s (&s):

When the actual parameter is passed to the formal parameter, the actual parameter is only a temporary copy of the actual parameter, and the modification of the formal parameter will not affect the actual parameter

The above is to use the method of structure variable. member name to store data

In fact, it can also be like this: the pointer directly points to the member name to understand better

ps->age=18;
strcpy(ps->name,"zhangsan");

Next, [write a function to print the data in s] : here you can pass the parameter directly to the structure variable s, but you don’t need to bring it back to the main function for printing. In fact, you can also pass the address here, which is actually a better choice

struct S
{
	char name[20];
	int age;
};
void print_s(struct S t)
{
	printf("%s %d\n", t.name, t.age);
}
void set_s(struct S* ps)
{
	ps->age = 18;
	strcpy(ps->name, "zhangsan");//字符串拷贝
}
int main()
{
	struct S s = { 0 };
	/*写一个函数给s种存放数据*/
	set_s(&s);
	//写一个函数打印s中的数据
	print_s(s);
	return 0;
}

[Summary]: When passing parameters to the structure, the address of the structure should be passed

(1) When the variable is passed when passing the parameter, we can do very little, because the formal parameter is only a temporary copy of the actual parameter, and the operation on the formal parameter does not change the actual parameter; but directly passing the address to the past , we can do a lot

(2) When we pass variables, copying an actual parameter also requires space ;

And directly pass the address, directly operate on the original parameters, no extra space is needed , which greatly improves the efficiency

 That’s all for the content of the structure this time. If you have any questions, welcome to the comment area or private message to communicate. I think the author’s writing is okay, or I have gained a little bit. Lian, thank you very much!

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/132021971