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

边栏推荐
猜你喜欢

文件系统的简单操作

Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch

2022 Hangzhou Electric Power Multi-School League Game 5 Solution

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

JVM笔记

Polygon zkEVM网络节点

y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
SQL injection in #, - +, - % 20, % 23 is what mean?

将xml标签转换为txt(voc格式转换为yolo方便进行训练)

Oracle与Postgresql在PLSQL内事务回滚的重大差异
随机推荐
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
深度学习之 10 卷积神经网络3
7-2 LVS+DR概述与部署
Polygon zkEVM network node
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
如何动态添加script依赖的脚本
Eight guiding principles to help businesses achieve digital transformation success
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
网络工程师入门必懂华为认证体系,附系统学习路线分享
How to systematically plan and learn software testing?
基本表单验证流程
Mobile payment online and offline payment scenarios
How to automatically export or capture abnormal login ip and logs in elastic to the database?
企业直播风起:目睹聚焦产品,微赞拥抱生态
mysql index notes
mysql索引笔记
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
SQL injection in #, - +, - % 20, % 23 is what mean?
Oracle与Postgresql在PLSQL内事务回滚的重大差异
2003. 每棵子树内缺失的最小基因值 DFS