当前位置:网站首页>C语言 结构体定义方法
C语言 结构体定义方法
2022-08-02 03:22:00 【a只如初见】
结构声明(也见有称做定义一个结构体)是描述结构如何组合的主要方法。
一般形式是:
struct 结构名{
成员列表
};
struct关键词表示接下来是一个结构。
下面归纳几种定义结构体变量的方法。
第一种是最基本最标准的结构体定义,即先定义结构体类型,再定义结构体变量。
struct student{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
};
struct student stu1,stu2; //此时stu1,stu2为student结构体变量
后面再定义新的变量也是struct student做类型名,如 struct student stu3;
第二种 定义结构体类型的同时定义结构体变量。
struct student{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
} stu1,stu2;
此后还可以继续定义student结构体变量,如:
struct student stu3;
第三种 不指定结构体名而直接定义结构体变量。
struct{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
} stu1,stu2;
一般不使用这种方法,因为直接定义结构体变量stu1、stu2之后,就不能再继续定义该类型的变量。
没有typedef的方法都例举完了,后面都是带typedef的方法。
第四种 先定义结构体类型,再定义结构体变量(有结构体名)。
typedef struct student{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
}student_t;
student_t stu1,stu2; //此时stu1,stu2为student结构体变量
struct student stu3;
后面再定义新的变量用student_t 或 struct student 做类型名都可以。
student_t 是一个别名。
第五种 先定义结构体类型,再定义结构体变量(无结构体名)。
typedef struct{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
}student_t;
student_t stu1,stu2; //此时stu1,stu2为student结构体变量
与第四种不同的是后面再定义新的变量只能用student_t 做类型名。
还有一种没有什么价值,相当于typedef白用了呗,效果与第一种方法无异。
typedef struct student{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
};
struct student stu1,stu2; //此时stu1,stu2为student结构体变量
实用情况中,第五种用得最多。
可以将下面这段 定义写在头文件中,供每个包含该头文件的源文件使用,定时变量的时候student_t stu1即可。
typedef struct{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
}student_t;
也可以将下面这段 定义写在头文件中,供每个包含该头文件的源文件使用,定时变量的时候struct student stu1即可,大部分人可能会觉得student_t 比 struct student 看起来更加简便。
struct student{
char no[20]; //学号
char name[20]; //姓名
char sex[5]; //性别
int age; //年龄
};
参考鸣谢:
https://blog.csdn.net/please_fix_/article/details/104595440
https://yum9193.blog.csdn.net/article/details/81190361
https://zhuanlan.zhihu.com/p/521248221
https://wenku.baidu.com/view/4ffde1d6f51fb7360b4c2e3f5727a5e9856a270f.html
边栏推荐
- 分布式领域最重要的一篇论文,到底讲了什么?
- Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
- Debian 10 NTP 服务配置
- [Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
- Redis简单学习笔记
- Problems when yolov5 calls ip camera
- subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.
- ssm各类配置模板
- 小程序组件总结
- 活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
猜你喜欢
parser = argparse.ArgumentParser()解析
mysql8.0安装教程与配置(最详细)操作简单
Amazon sellers how to improve the conversion
2022年比若依更香的开源项目
Problems when yolov5 calls ip camera
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
Mysql8创建用户以及赋权操作
Redis简单学习笔记
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
随机推荐
【C语言万字长文】 宏定义 结构体 共用体 内存对齐知识点总结
连接数据库时遇到的bug1号
删库后!除了跑路还能干什么?
二维数组实战项目--------《三子棋》
磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
知识问答与知识会话的区别
2022年比若依更香的开源项目
Chapter 10 Clustering
[详解C语言]一文带你玩转C语言小游戏---三子棋
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
STL entry basics map and set containers
【面试】失败的一次面试
ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
Week 7 Review
Redis的集群模式
构造方法、方法重载、全局变量与局部变量
@ApiModel 和 @ApiModelProperty
MySQL分库分表