当前位置:网站首页>结构体指针知识要点总结
结构体指针知识要点总结
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);
}运行结果:

边栏推荐
- 【id类型和NSObject指针 ObjectIve-C中】
- 看DevExpress丰富图表样式,如何为基金公司业务创新赋能
- [Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
- SQL query String field less than 10 how to check
- Explain detailed explanation and practice
- 杭电多校-Slipper-(树图转化+虚点建图)
- 劝退背后。
- XSS相关知识点
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- 十一种概率分布
猜你喜欢

JVM Notes

TL431的基本特性以及振荡电路

转:管理是对可能性的热爱,管理者要有闯进未知的勇气

技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris

Explain详解与实践

2022杭电多校联赛第五场 题解

劝退背后。

Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model

机器学习模型的“可解释性”

Take care of JVM performance optimization (own note version)
随机推荐
Mockito unit testing
sql语句查询String类型字段小于10的怎么查
Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
函数,递归以及dom简单操作
2.15 keil使用电脑端时间日期
TL431的基本特性以及振荡电路
PL/SQL Some Advanced Fundamental
7-1 LVS+NAT load balancing cluster, NAT mode deployment
7-3 LVS+Keepalived集群叙述与部署
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
基地址:环境变量
Mockito单元测试
马尔可夫链
汇编语言之栈
drools从下载到postman请求成功
docker+bridge+redis master-slave+sentry mode
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
机器学习模型的“可解释性”
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
7. The principle description of LVS load balancing cluster