码迷,mamicode.com
首页 > 其他好文 > 详细

结构体的简单使用

时间:2020-07-01 00:31:11      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:数组   char   简单的   class   一个   结构体   int   student   type   

因为后面要学习数据结构(DataStruct)需要使用到结构体(Struct),所以我们先复习一下结构体(因为好久没写c语言了,有点忘记了,所以想简单的复习一下)

struct Var {
    基本数据类型 var1;
    基本数据类型 var2;
    基本数据类型 var3;

};

利用struct关键字定义结构体,下面我们来看一个例子

struct Student {
    char xm[10];
    char xh[10];
    int age;
};

这个例子定义了一个学生结构体类型,有一个字符数组姓名(xm),一个字符数组学号(xh),一个整数类型的年龄(age);形成一个结构体类型

结构体常常与typedef一起用,就是起一个别名,之后创建变量写起来更简单

1.没有用typedef的创建一个结构体变量

struct Student {
    char xm[10];
    char xh[10];
    int age;
};

struct Student student;

2.用typedef的创建一个结构体变量

typedef struct Student {
    char xm[10];
    char xh[10];
    int age;
    }stu;

stu student;

可以看出使用typedef起别名可以使我们创建结构体变量时,不需要写struct Student,直接写stu,就可以了

 

后面的数据结构,会经常使用到结构体,比如定义链表的节点

typedef struct Node {
    ElementType Data;
    struct Node* next;
}nd,
*nd;

 

大家一起加油啊,好好学习数据结构!

 

结构体的简单使用

标签:数组   char   简单的   class   一个   结构体   int   student   type   

原文地址:https://www.cnblogs.com/gitpy123/p/13216905.html

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