zoukankan      html  css  js  c++  java
  • struct和typedef struct的区别

    在了解struct和typedef struct之前,我们先学习下typedef的定义和用法。
    typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。
    C语言里typedef的解释是用来声明新的类型名来代替已有的类姓名,例如:
    typedef int CHANGE;
    指定了用CHANGE代表int类型,CHANGE代表int,那么:
    int a,b;和CHANGE a,b;是等价的、一样的。方便了个人习惯,熟悉的人用CHANGE来定义int。

    现在回到struct和typedef struct的区别这个问题上来

    • 用struct node{}来定义结构体时,当我们定义node的结构体变量是,写法为:struct node n;
    • 用typedef struct node{}NODE来定义结构体时,申请变量时,写法为:NODE n,这里NODE就相当于node的别名。

    归纳起来就是在使用时,是否可以省去struct这个关键字
    举例来说:
    在C中定义一个结构体类型时如果要用typedef:

    typedef struct Student
    {
       int no;
       char name[12];
    }Stu,student;
    

    于是在声明变量的时候就可:Stu stu1;或者:student stu2;(Stu 和student 同时为Student的别名)
    如果没有typedef即:

    struct Student
    {
       int no;
       char name[12];
    }Stu;
    

    就必须用struct Student stu1;或者struct Stu stu1来声明
    另外这里也可以不写Student

    typedef struct
    {
       int no;
       char name[12];
    }Stu;
    

    参考:https://blog.csdn.net/qq_41848006/article/details/81321883

  • 相关阅读:
    HTML5中video的使用一
    sql存储过程的简单使用
    Linq to Sqlite连接
    linq to sql简单使用
    练习笔记:net,JqueryUI实现自动补全功能
    三个师妹之出题
    Docker——questions
    Docker——网络
    Docker——dockerfile
    Docker——容器数据卷
  • 原文地址:https://www.cnblogs.com/pogeba/p/14073039.html
Copyright © 2011-2022 走看看