码迷,mamicode.com
首页 > 编程语言 > 详细

C语言结构体,点运算和箭头运算

时间:2016-07-29 21:27:07      阅读:538      评论:0      收藏:0      [点我收藏+]

标签:

C语言有一种数据类型叫结构体,其定义格式为:

  struct 结构体名 {
    结构体成员变量定义;
  };
  如:
  struct student {
  char name[20];
  int age ;
  double score ;
  };

  用结构体定义的变量叫结构体变量,如:

      struct student stu; //定义一个结构体变量stu

  这种变量在引用结构体成员时,使用点(.)来操作,如:

  strcpy( stu.name, "zhangsan" );
  stu.age=20;
  stu.score=100;

  结构体类型也可以定义指针变量,如:

      struct student *pstu; //定义一个结构体指针变量pstu

      pstu=&stu ; //pstu指针指向stu结构体变量

  结构体指针变量在引用成员变量时,使用箭头(->)来操作,如:

  strcpy( pstu->name, "zhangsan" );
  pstu->age=20;
  pstu->score=100;

  以上这两种符号的使用是C语言的规定!

typedef struct abc
{int x;
int y;
int z;
}ABC;
是将结构体abc类型重新起个名字为ABC,以后再定义同一类型的变量时,可以写成:
ABC m,n;
与:
struct abc m,n;
作用是一样的。
可将复杂数据类型简单化

C语言结构体,点运算和箭头运算

标签:

原文地址:http://www.cnblogs.com/judes/p/5719595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!