1.(1)数据结构之链表-typedef的用法

本人坚持更新C语言,数据结构,操作系统知识,可以收藏+关注随时了解😜😜😜 


目录

我们在之前学习结构体的时候,是如何定义结构体的呢?

typedef的定义

typedef的用法

方法1

方法2

方法3


我们在之前学习结构体的时候,是如何定义结构体的呢?

    struct Student
    {
        int age;
        float score;
        char sex;
    };//定义数据类型
    
    struct Student stu ={10,98.5,'M'}; 

我们往往先是定义数据类型struct Student,然后再通过struct Student stu来定义变量

但是这样的缺点是我们每次定义变量的时候,都需要写struct Student,这样会显得代码杂乱,

这里就需要使用到typedef

typedef的定义

typedef 关键字来定义自己习惯的数据类型名称,来替代系统默认的基本类型名称、数组类型名称、指针类型名称与用户自定义的结构型名称、共用型名称、枚举型名称等。

typedef int ZHANGSAN

这句话的意思就是,将int 重新起了一个名字叫ZHANGSAN,此时int i= 5;就等价于ZHANGSAN i=5.

当然,typedef在C语言中,多和结构体在一起连用

typedef的用法

方法1:

typedef struct Student
{
    int sid;
    char name[100];
    char sex;
} ST; //为struct Student重新起一个名字叫ST

int main(){
    ST stu ={100;"bozhubenren",'M'};
    return 0;
}

我们在这里为数据类型struct Student重启名字为ST,此时struct Student <=> ST

方法2:

typedef struct Student
{
    int sid;
    int age;
} * PSTU; // PST 等价于struct Student*

int main(){
    struct Student stu;
    PSTU p = &stu;
    p->sid = 100;
    return 0;
}

我们在这里为数据类型struct Student*重启名字为PSTU,此时struct Student* <=>PSTU

方法3:

typedef struct Student
{
    int sid;
    int age;
} * PSTU, STU;

int main(){
    STU st;
    PSTU p = &st;
    p->sid = 100;
    return 0;
}

我们在这里为数据类型struct Student重启名字为ST,此时struct Student <=> ST.

struct Student*重启名字为PSTU,此时struct Student* <=>PSTU

  • 20
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱打羽毛球的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值