结构体指针

指向结构体变量的指针

例如:struct student *p  //p可以指向struct student 类型的变量或数组元素;

例题:

#include <stdio.h>
main()
{
 struct student
  {
    char name[20];     //姓名
    int num;        //学号
    int age;        //年龄
    char group;      //所在小组
    float score; //成绩
  } stu1 = { "Tom", 12, 18, 'A', 136.5 },*pstu = &stu1;          //读取结构体成员的值
    printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", (*pstu).name, (*pstu).num, (*pstu).age, (*pstu).group, (*pstu).score);   //括号不可省略,点运算符优先级高于星号运算符
    printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", pstu->name,pstu->num, pstu->age, pstu->group,pstu->score);      //"->"指向运算符,表示p所指向的结构体变量的某个成员

}

输出结果:

      Tom的学号是12,年龄是18,在A组,今年的成绩是136.5!
      Tom的学号是12,年龄是18,在A组,今年的成绩是136.5!

定义结构体数组:

      struct student stu[3];

 

posted @ 2017-12-07 19:16  _痴人说梦  阅读(1219)  评论(0编辑  收藏  举报