typedef:
typedef用来干什么:
我们常用typedef来赋予数据类型一个新的名字,来方便我们的使用
typedef的用法:
typedef 数据类型 新的名字;
举例说明:
代码:
#include<iostream>
using namespace std;
int main()
{
typedef int zheng_shu;
zheng_shu a = 1;
int b = 1;
cout << a <<endl<< b;
return 0;
}
结果:
解释:
我们可以看到我们可以通过这种方式用zheng_shu来代替int。
结构体:
什么是结构体:
结构体顾名思义,是我们定义一种数据的结构。比如我们要统计一个学生的信息,便可以用结构体来构造其数据结构。
结构体的构造:
struct 结构体名称
{
结构体的主体;
};
举例:
代码:
struct student
{
char name;
int num;
};
//此处的;必须写
结构体的使用:
当我们要使用这种结构体时,我们要用如下方法:
struct 结构体名称 对象名称;
举例:
代码:
struct student stu;
或者:
struct student
{
char name;
int num;
}stu;
结构体的访问:
当我们访问结构体内的内容时我们有两种方式( . 和 ->)。
1. 对象名称.结构体内的内容
2. 对象名称->结构体内的指针
两者的区别在于:
“ . ”操作符左边的操作数是一个“结构体”的表达式,而“ -> ”操作符左边的操作数是一个指向结构体的指针。
即->只用于结构体内的指针。
代码:
cout<<stu.num;
结构体的初始化:
整体初始化:
struct student stu = {
'z',
1
};
逐个初始化:
stu.name = 'z';
stu.num = 1;
typedef与结构体相结合:
我们在使用结构体时,如果每次都使用struct student stu;则显得繁琐,于是我们可以结合typedef来简化我们的操作。
代码:
typedef struct student
{
char name;
int num;
};
解释:
标签:typedef,struct,--,stu,int,student,数据结构,结构 From: https://blog.csdn.net/qq_60624992/article/details/136986898在我们结构体定义前加上typedef即可,以后使用结构体时只需要用student stu;即可。