例程中这个“->”符号怎么理解啊?
看咱家的例程,如图中标记处,这个“->”符号怎么理解啊?
本帖最后由 VCC 于 2024-12-3 15:18 编辑
这是结构体语法哦!
关于结构体成员的访问
s是一个struct student 结构体
ps是个struct student 结构体指针,指向s的地址
struct student有个char型成员叫name
那么:
使用.操作符访问成员变量,当通过结构体变量访问成员时使用:
struct student s;s.name = 'K';
使用->操作符访问成员变量,当通过结构体指针访问成员时使用:
struct student *ps = &s;ps->name = 'K';
VCC 发表于 2024-12-3 15:10
这是结构体语法哦!
谢老大回复,我还要加强学习。 这是结构体指针访问成员的语法,->是结构体指针访问,.是结构体变量访问,例如:
typedef struct class student_t{
int id;
int age;
} Student;
Student st1;
st1.id=10; //这是.访问结构体变量的写法
st1.age=15;
Student *p;
p=&stu1;
p->id=10;//这是->访问结构体指针的写法
p->id=15;
(*p).age 和 p->age等价的
学习了->这是结构体语法
s是一个struct student 结构体
ps是个struct student 结构体指针,指向s的地址
struct student有个char型成员叫name
那么:
使用.操作符访问成员变量,当通过结构体变量访问成员时使用:
struct student s;s.name = 'K';
使用->操作符访问成员变量,当通过结构体指针访问成员时使用:
struct student *ps = &s;ps->name = 'K';
页:
[1]