C 语言笔记
  • Introduction
  • First Chapter
  • 第一个程序
  • 变量与数据类型
    • puts 与 printf
    • 整数(short,int,long)
    • 小数(float,double)
    • 字符(char)与字符串
    • 数据类型转换
    • 自增(++)和自减(--)
    • 变量和数据类型
  • 输入输出
    • 数据输出函数
    • 从键盘输入数据
  • 附录*关键字及其解释
  • 附录*转义字符
  • 附录*进制转换
  • 附录*单位换算
  • 预处理命令
  • 结构体
    • -> 访问符意思
  • 指针
    • 指针与二维数组
    • 函数指针(指向函数的指针)
    • 彻底攻克C语言指针
    • 指针的总结
  • 资料
Powered by GitBook
On this page

Was this helpful?

  1. 结构体

-> 访问符意思

->在C语言中称为间接引用运算符,是二目运算符,优先级同成员运算符“.”搜索。

用法:

p->a,其中p是指向一个结构体的指针,a是这个结构体类型的一个成员。表达式p->a引用了指针p指向的结构体的成员a。

例如:

struct T
{
 int a;
 char b;
}s;

struct T *p=&s;

那么,

`p->a`相当于`s.a`。

显然,有个等价写法:(*p).a,和p->a完全等效。

#include <stdio.h>
int main(){
    struct{
        char *name;  //姓名
        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);

    return 0;
}

运行结果:

Tom的学号是12,年龄是18,在A组,今年的成绩是136.5!
Tom的学号是12,年龄是18,在A组,今年的成绩是136.5!
Previous结构体Next指针

Last updated 5 years ago

Was this helpful?