当前位置:网站首页>自学结构体(小甲鱼c语言)
自学结构体(小甲鱼c语言)
2022-06-29 16:58:00 【Fecter11】
以下内容是在b站上看的小甲鱼c语言时随着视频记录的代码
到底什么式结构体,怎么使用结构体?
以下面的代码为例子
# include <stdio.h>
struct Book //定义一个结构体的关键字struct 以及结构体的名称为Book注意通常首字母大写
{
char title[128]; //我们这个函数是为了存放书的名称,作者,以及价格,版号
char author[48]; //大括号内部是结构体成员
float price;
unsigned int date;
char pulisher[48];
}book; //这是结构体变量的名称,类似于 int book 中的book是整形的变量名,这里的book是结构体类型,结构名为Book的变量book。这里就有一点绕了。
void main(void)//主函数
{
While(1);//暂时啥也不做
}
另一个定义结构体的方法是
# include <stdio.h>
struct Book //定义一个结构体的关键字struct 以及结构体的名称为Book注意通常首字母大写
{
char title[128]; //我们这个函数是为了存放书的名称,作者,以及价格,版号
char author[48];
float price;
unsigned int date;
char pulisher[48];
}; //注意这里只定义了结构体的名称,而没有创建变量。
void main(void)//主函数
{
Struct Book book;//这里是创建了一个Book结构体类型的变量book
}
接下来就是怎样访问结构体变量?
访问结构体的成员引入一个新的符号一个“点号”——“.”
比如book.title就是引用Book结构体的title成员,显然title成员是一个字符数组。
而book.price是引用Book结构体的price成员,而price是一个浮点型的变量。
接下来继续看程序
# include <stdio.h>
struct Book
{
char title[128];
char author[48];
float price;
unsigned int date;
char pulisher[48];
}book;
void main(void)
{
printf("请输入书名 \n");
scanf("%s",book.title); //这里就涉及到赋值
printf("请输入作者 \n");
scanf("%s",book.author);
printf("请输入售价 \n");
scanf("%f",&book.price);
printf("请输入出版日期 \n");
scanf("%d",&book.date);
printf("请输入出版社 \n");
scanf("%s",book.pulisher);
printf("************数据录入完毕************\n");
printf("书名:%s\n",book.title);
printf("作者:%s\n",book.author);
printf("售价:%f\n",book.price);
printf("出版日期:%d\n",book.date);
printf("出版社:%s\n",book.pulisher);
}
初始化结构体变量
初始化一个变量和一个数组
Int a = 520;
Int array[5]={
1,2,3,4,5};
初始化一个结构体变量
Struct Book book ={
“小甲鱼带你飞”, //注意用逗号分隔,另外注意一定要与定义的结构体成员类型对应起来
“小甲鱼”,
9.9,
20170707,
“清华”
};
初始化结构体的指定成员值
其语法与数组指定初始化元素类似,不过结构体指定初始化成员使用“点号”——“.”运算符和成员名
比如初始化book的price成员:
Struct Book book = {
.price = 9.9};
还可以不按结构体声明的成员顺序进行初始化:
Struct Book book = {
.publisher = “清华”,
.price = 9.9,
.date = 20170707
};
系统会对内存进行对齐操作
# include <stdio.h>
void main (void)
{
struct A
{
char a;
int b;
char c;
}a = {
'x',250,'c'};
printf("size of a = %d \n",sizeof(a));
}
理论上上述程序的x站一个字节,250占4个字节,c占一个字节
实际输出为12个字节
每个都按照4个字节处理的旧成了12个字节
如果改变输出的顺序x,o,250
则输出为8个字节
因为,前两个字符型都各占2个字节,但是系统自动补齐2个字节,总共占4个字节,而250本身就分配了4个字节。
结构体数组和结构体指针
结构体嵌套
# include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;//结á构1体?的?嵌?套?
char pulisher[48];
}book = {
"小甲鱼,
"小甲鱼",
9.9,
{
2017,1,11},
"清华大学"
};
void main(void)
{
printf("书名:\n",book.title);
printf("作者:%s\n",book.author);
printf("售价:%f\n",book.price);
printf("出版日期:%d-%d-%d\n",book.date.year,book.date.month,book.date.day); //注意一定要访问到最底层才可以
printf("出版社:%s\n",book.pulisher);
}
结构体数组
第一种:在声明结构体的时候进行定义
Struct 结构体名称
{
结构体成员;
}数组名[长度];
第二种:先声明一个结构体类型(比如上面的Book),再用此类型定义一个结构体数组。
Struct 结构体名称
{
结构体成员;
};
Struct 结构体名称 数组名[长度];
初始化结构体数组
Struct Book book[3] = {
{“<零基础>”,”小甲鱼”,9.9,{2017,11,7},”清华”},
{“入门”,”甲鱼”,9.9,{2017,10,1},”清华”},
{“到入土”,“鱼”,9.9,{2017,9,9},”清华”},
};
结构体指针
Struct Book * pt; //声明一个Book类型结构体的指针变量pt
这里需要注意一点,数组的变量名代表了该数组的首地址,所以数组名可以直接赋值给指针变量。而结构体指针的变量名需要使用“&”才可以将地址给结构体变量。
还需要加一个 pt = &book;
通过结构体指针访问结构体成员有两种方法:
第一种:
(结构体指针).成员名
注意:这里加括号是因为“.”运算符的优先级要高于“”运算符。
第二种:
结构体指针->成员名
注意“->”之间不能有空格,另外他的优先级与“.”的优先级是一样的。
以上两种是完全等价的
第一种引用方式如下:
# include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;//结á构1体?的?嵌?套?
char pulisher[48];
}book = {
"小甲鱼飞,
"小甲鱼",
9.9,
{
2017,1,11},
"清华大学"
};
void main(void)
{
struct Book*pt;
pt = &book;
printf("书名:%s\n",(*pt).title);
printf("作者:%s\n",(*pt).author);
printf("售价:%0.2f\n",(*pt).price);
printf("出版日期:%d-%d-%d\n",(*pt).date.year,(*pt).date.month,(*pt).date.day);
printf("出版社:%s\n",(*pt).pulisher);
}
第二种引用方式
# include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;//结构体的嵌套
char pulisher[48];
}book = {
"小甲鱼飞,
"小甲鱼",
9.9,
{
2017,1,11},
"清华大学"
};
void main(void)
{
struct Book*pt;
pt = &book;
printf("书名:%s\n",pt->title);
printf("作者:%s\n",pt->author);
printf("售价:%0.2f\n",pt->price);
printf("出版日期:%d-%d-%d\n",pt->date.year,pt->date.month,pt->date.day); printf("出版社:%s\n",pt->pulisher);
}
边栏推荐
- When MySQL RDS is collected using Flink CDC, the datetime type field will be compared with the source table after collection
- 知道创宇为能源行业资产管理助力,入选工信部2021物联网示范项目
- 力扣解法汇总535-TinyURL 的加密与解密
- 研究所的这些优势真香!上岸率还极高!
- 6.25atcoderabc257e - addition and multiplication 2
- 基于C语言开发实现的一个用户级线程库
- Word2vec vector model of Wiki Chinese corpus based on deep learning
- sectigo ov泛域名证书一年一千五百九十元好用吗
- How to configure logback? 30 minutes for you to thoroughly learn the code to stay up late and knock
- Fluent的msh格式网格学习
猜你喜欢

最高81.98%!超百所“双一流”高校本科深造率公布

如何创建虚拟形象

Calibration of binocular camera based on OpenCV

Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?

Naacl 2022 | distillation of machinetranslation SOTA model

垃圾收集器

Perhaps in two years, ASML will be free to supply EUV lithography machines to China

Us chips are hit hard again, and Intel may be defeated by TSMC and reduced to the third place in the world

自旋电子学笔记-张曙丰

【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
随机推荐
6.25atcoderabc257e - addition and multiplication 2
深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
机器学习8-人工神经网络
机器学习7-支持向量机
【R语言数据科学】:文本挖掘(以特朗普推文数据为例)
力扣解法汇总535-TinyURL 的加密与解密
ICML 2022 | transferable imitation learning method based on decoupling gradient optimization
Gradle download slow or unable to download
Summary of problems during xampp Apache installation
Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
Practice | solution for image upload, rotation and compression on mobile terminal
自旋电子学笔记-张曙丰
curl: (56) Recv failure: Connection reset by peer
Inheritablethreadlocal resolves message loss during message transmission between parent and child threads in the thread pool
redolog和binlog
Redis布隆过滤器和布谷鸟过滤器
In order to prevent being rectified after 00, a company requires employees not to sue the company
Function calculation asynchronous task capability introduction - task trigger de duplication
Redis bloom filter and cuckoo filter
GNN notes: message propagation model