对struct访问成员的理解

来源:互联网 发布:linux常用软件下载 编辑:程序博客网 时间:2024/05/18 03:56

struct的成员访问有两种方式:

a),  .

b),  ->

前提条件:

    struct my_struct
    {
int id;
char name[0]
    };

struct  my_struct s1, *p_s1;

p_s1 = &s1;


s1.id = 1;

(*p_s1 ).id = 1; // ()必须加!!!

注意这里,“.”方式的左值是结构。上面两种方式是等价的。


p_s1 ->id = 1;

这里“->”的左值是指针。