当前位置:网站首页>结构体指针知识要点总结
结构体指针知识要点总结
2022-08-04 04:11:00 【BSP初级小学僧】
博客主页:https://blog.csdn.net/weixin_46094737?type=blog
欢迎评论留言 如有错误敬请指正!
本文由小学生廉原创,首发于 CSDN
未来很长,值得我们全力奔赴更美好的生活!
首先是typedef关键字的用法:
基本定义:
typedef为C语言的关键字,作用是为一种数据类型 定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。 在编程中使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明。
用途一:与#define的区别
typedef 行为有点像 #define 宏,用其实际类型替代同义字。不同点是 typedef 在编译时被解释,因此让编译器来应付超越预处理器能力的文本替换。
用途二:减少错误
定义一种类型的别名,而不只是简单的宏替换。可以用作同时声明指针型的多个对象。
————————————————
版权声明:本文为CSDN博主「BSP初级小学僧」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46094737/article/details/126136629
typedef struct //关键字typedef
{
int id;
int score;
}Stu_i;//爱称(相当于数据类型)
Stu_i stu01={2202,89},stu02;
另外typedef在使用时,可以省略结构体变量名,可以看到在这里我们是直接不写结构体变量名
typedef struct //关键字typedef
可以看到如果没有使用typedef关键字的时候,这个就是一个匿名结构体。
在不使用typedef关键字的时候,编译器会出现警告,只限于有单个结构体变量时,在出现多个结构体变量时,程序会直接报错,这里切记切记。
2、结构体指针的嵌套用法
typedef struct
{
int id;
char name[10];
bir *pbir;
}stu_i;
例如,要统计一个学生的信息,信息包含学号、名字和出生日期。但是出生日期又包含了年月日三个信息,一一声明当然也可以,这里使用指针嵌套的方法进行声明。
1、先声明一个带有指针的结构体,代码见上。
2、再声明一个出生日期的结构体
typedef struct
{
int year;
int month;
int day;
}bir;
3、将指针指向含有出生日期信息的变量地址
stu_i stu01=
{
.id=2022,
.name="夏洛特",
.pbir=&bir_test
};
4、初始化、然后打印。
源代码:
#include <stdio.h>
#include <string.h>
void struct_test(void);
int main()
{
struct_test();
return 0;
}
void struct_test(void)
{
typedef struct
{
int year;
int month;
int day;
}bir;
typedef struct
{
int id;
char name[10];
bir *pbir;
}stu_i;
bir bir_test=
{
.year=2022,
.month=7,
.day=25
};
stu_i stu01=
{
.id=2022,
.name="夏洛特",
.pbir=&bir_test
};
printf("%d\n",stu01.id);
printf("%s\n",stu01.name);
printf("%d-%d-%d",stu01.pbir->year,stu01.pbir->month,stu01.pbir->day);
}
运行结果:
边栏推荐
- 函数,递归以及dom简单操作
- 打造一份优雅的简历
- 基本表单验证流程
- Oracle与Postgresql在PLSQL内事务回滚的重大差异
- Polygon zkEVM网络节点
- Explain详解与实践
- Embedded database development programming MySQL (full)
- 元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
- 6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
- 系统设计.秒杀系统
猜你喜欢
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
manipulation of file contents
小程序 + 电商,玩转新零售
The Shell function
文件内容的操作
【21天学习挑战赛】图像的旋转问题(二维数组)
10 Convolutional Neural Networks for Deep Learning 3
深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
随机推荐
帮助企业实现数字化转型成功的八项指导原则
2022年软件测试——精选金融银行面试真题
8. Haproxy builds a web cluster
Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
文件内容的操作
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
数组相关 内容 解析
MySQL查询优化与调优
图像处理之Bolb分析(一)
2022 Hangzhou Electric Power Multi-School League Game 5 Solution
Explain detailed explanation and practice
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
sql语句查询String类型字段小于10的怎么查
Use serve to build a local server
mysql index notes
"Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
嵌入式数据库开发编程MySQL(全)
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
MySQL query optimization and tuning