-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct
More file actions
39 lines (37 loc) · 1.16 KB
/
Copy pathstruct
File metadata and controls
39 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
struct Book
{
char name[5];
int id;
float price;
};
struct Student
{
struct Book book;
//成员变量
char name[20];
int age;
char sex[5];
} s1, s2; //s1和s2也是结构体变量,不过是全局变量
void print1(struct Student t)
{
printf("%s %d %lf %s %d %s\n", t.book.name, t.book.id, t.book.price, t.name, t.age, t.sex);
}
void print2(struct Student *ps)
{
printf("%s %d %lf %s %d %s\n", ps->book.name, ps->book.id, ps->book.price, ps->name, ps->age, ps->sex);
}
int main()
{
struct Student s = {{"book", 12345, 88.88}, "lisa", 18, "female"}; //创建对象,局部变量
// printf("%s\n", s.book.name);
// struct Student *ps = &s;
// printf("%s\n", (*ps).name);
// printf("%s\n", ps->name);
//写一个函数打印s里的内容
// print1(s); //传值调用
// print2(&s); //传址调用--效率高,节约空间,可以改s中的数据
// //首选print2函数,原因:函数传参的时候,参数需要压栈。如果传递一个结构体对象,结构体过大,参数压栈的系统开销比较大,导致性能的下降
// //结论:结构体传参的时候,要传递结构体的地址
return 0;
}