结构体的简单使用

因为后面要学习数据结构(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,*node;

 

 

好了,我们下回见,peace

 

posted @ 2020-07-01 00:00  野评测  阅读(181)  评论(0)    收藏  举报