一、”.”和”->”的区别

  • 对于结构体指针,应该使用”->”
  • 对于结构体,应该使用”.”‘

举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Student {
int age;
char name[20];
};

// 定义结构体变量
struct Student s;
s.age = 20; // 直接通过变量访问成员
strcpy(s.name, "Alice");

struct Student *ptr;
ptr = &s; // ptr指向结构体变量s
ptr->age = 21; // 通过指针访问成员
// 等价于 (*ptr).age = 21;