primary structure
1. Declaration of structure
1.1 Basic knowledge of structure
A structure is a collection of values called member variables . Each member of the structure can be a variable of different types
Structure - -> (structure)
comparison:
1.2 Declaration of structure
struct Stu
{
//学生相关属性
char name[20];
int age;
char sex[5];
char id[20];
}s3,s4;//不可省略;
//s3,s4是结构体变量类型
//s3,s4是全局的
int main()
{
struct Stu s1;
struct Stu s2;
//s1,s2是结构体类型的变量,是局部的
struct Stu s3;
return 0;
}
The type definition is indispensable for struct
Let's look at the following definition: (struct can be omitted)
typedef struct Stu
{
//学生相关属性
char name[20];
int age;
char sex[5];
char id[20];
}Stu;//Stu是重新定义的新类型
int main()
{
struct Stu s1;
struct Stu s2;
//s1,s2是结构体类型的变量,是局部的
Stu s5;//typedef定义下可省略struct
return 0;
}
1.3 Types of structure members
Members of structure variables can be scalars, arrays, pointers, or even other structures
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 Initialization and definition of structure variables
With the structure type, how to define variables is actually very simple.
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', 99}, NULL };//局部变量
//指定成员来初始化
struct S s4 = {
.num = 1000, .arr = {
1,2,3,4,5} };//局部变量
return 0;
}
2. Structure access
- Struct Variable Access Members
Struct variable members are accessed through the dot operator (.). The dot operator accepts two operands. - Struct pointer access to members of variables
Sometimes what we get is not a struct variable, but a pointer to a struct. Access 1 with (->)
Case 1:
#include <string.h>
struct S
{
char name[20];
int age;
};
void set_s(struct S t)
{
t.age = 18;
//t.name = "zhangsan";//err, 因为name是数组名,数组名是常量的地址
strcpy(t.name, "zhangsan");//字符串拷贝
}
int main()
{
struct S s = {
0 };
//写一个函数给s中存放数据
set_s(s);
return 0;
}
wrong code above
Modify as follows:
//void set_s(struct S* ps)
//{
// (*ps).age = 18;
// //t.name = "zhangsan";//err, 因为name是数组名,数组名是常量的地址
// strcpy((*ps).name, "zhangsan");//字符串拷贝
//}
//
void set_s(struct S* ps)
{
ps->age = 18;
//t.name = "zhangsan";//err, 因为name是数组名,数组名是常量的地址
strcpy(ps->name, "zhangsan");//字符串拷贝
}
void print_s(struct S* ps)
{
printf("%s %d\n", ps->name, ps->age);
}
int main()
{
struct S s = {
0};
//写一个函数给s中存放数据
set_s(&s);
//写一个函数打印s中的数据
print_s(&s);
return 0;
}
3. Structure parameter passing
Go directly to the code:
struct S
{
char name[20];
int age;
};
void set_s(struct S* ps)
{
ps->age = 18;
//t.name = "zhangsan";//err, 因为name是数组名,数组名是常量的地址
strcpy(ps->name, "zhangsan");//字符串拷贝
}
void print1(struct S ps)
{
printf("%s %d\n", ps.name, ps.age);
}
int main()
{
struct S s = {
0 };
//写一个函数给s中存放数据
set_s(&s);
//写一个函数打印s中的数据
print1(s);
return 0;
}
//
//
struct S
{
char name[20];
int age;
};
void set_s(struct S* ps)
{
ps->age = 18;
//t.name = "zhangsan";//err, 因为name是数组名,数组名是常量的地址
strcpy(ps->name, "zhangsan");//字符串拷贝
}
void print2(struct S* ps)
{
printf("%s %d\n", ps->name, ps->age);
}
int main()
{
struct S s = {
0};
//写一个函数给s中存放数据
set_s(&s);
//写一个函数打印s中的数据
print2(&s);
return 0;
}
The answer is: the print2 function is preferred.
reason:
When using a structure object, the structure is too large, and the system overhead of pushing parameters to the stack is relatively large, which will lead to a decrease in performance.
in conclusion:
When a structure is passed as a parameter, the address of the structure must be passed.
Unknowingly, the structure began to come to an end. You must have gained a lot from reading the full text, let us continue to forge ahead together for C language learning