2020.1.13 C语言学习 结构体+结构体数组+结构体指针
结构体
结构体的形式
- struct 结构体名称
{
结构体成员1;
结构体成员2;
…
};
例如: 图书的结构体
struct Book
{char title[128];char author[48];float price;unsigned int date;char publisher[40];
};
- 定义结构体类型变量
可以在结构体声明时 后面跟上变量,这个变量是全局变量
例如
struct Book
{char title[128];char author[48];float price;unsigned int date;char publisher[40];
}book;//boo是全局变量
也可以在主函数中定义
#include <stdio.h>struct Book
{char title[128];char author[48];float price;unsigned int date;char publisher[40];
};int main(void)
{struct Book book;//这个是局部变量return 0;
}
- 访问结构体变量
要访问结构体成员,我们需要引入一个新的运算符 —点号(.)运算符。比如book.title就是引用book结构体的title成员, 他是一个字符数组;而book.price 则是引用book结构体的price成员,它是一个浮点型的变量
例如 图书
#include <stdio.h>struct Book
{char title[128];char author[48];float price;unsigned int date;char publisher[40];
}book;int main(void)
{printf("请输入书名: ");scanf("%s", book.title);printf("请输入作者: ");scanf("%s", book.author);printf("请输入售价: ");scanf("%f", &book.price);printf("请输入出版日期: ");scanf("%d", &book.date);printf("请输入出版社: ");scanf("%s", book.publisher);printf("\n=======数据录入完毕=====\n");printf("书名: %s\n", book.title);printf("作者: %s\n", book.author);printf("售价: %.2f\n", book.price);printf("出版日期: %d\n", book.date);printf("出版社: %s\n", book.publisher);return 0;
}
结果输出
请输入书名: 老人与海
请输入作者: 海明威
请输入售价: 20
请输入出版日期: 20200113
请输入出版社: 中国矿业大学出版社=======数据录入完毕=====
书名: 老人与海
作者: 海明威
售价: 20.00
出版日期: 20200113
出版社: 中国矿业大学出版社
初始化结构体变量
初始化一个结构体变量:
struct Book book = {"老人与海","海明威";20,20200113"中国矿业大学出版社"
};
一定要对号入座
初始化结构体的指定成员值
结构体初始化成员使用点号(.)运算符和成员名
例如:让程序只初始化Book的price成员:
struct Book book = {.price = 20};
也可以不按结构体声明的成员顺序进行初始化:
struct Book book = {.publisher = "中国矿业大学出版社",.price = 20,.date = 20200113
};
结构体嵌套
嵌套例子:
#include <stdio.h>struct Date
{int year;int month;int day;
};struct Book
{char title[128];char author[40];float price;struct Date date;//嵌套char publisher[40];
}book = {"老人与海","海明威",20,{2020, 01, 13},"中国矿业大学出版社"
};
int main(void)
{printf("书名: %s\n", book.title);printf("作者: %s\n", book.author);printf("价格: %.2f\n", book.price);printf("日期: %d-%d-%d\n", book.date.year, book.date.month,book.date.day);printf("出版社: %s\n", book.publisher);return 0;
}
输出结果
书名: 老人与海
作者: 海明威
价格: 20.00
日期: 2020-1-13
出版社: 中国矿业大学出版社
结构体数组
第一种方法是在声明结构体的时候进行定义:
struct 结构体名称
{结构体成员1;结构体成员2;...
}数组名[长度];
第二种方法是先声明一个结构体类型,在用此类型定义一个结构体数组:
struct 结构体名称
{结构体成员1;结构体成员2;...
};
struct 结构体名称 数组名[长度];
初始化结构体数组
例子:
struct Book book[3] = {{"老人与海", "海明威", 20, {2020, 01, 13}, "中国矿业大学出版社"};{"老人与陆", "陆明威", 21, {2021, 01, 13}, "清华大学出版社"};{"老人与空", "空明威", 22, {2022, 01, 13}, "北京大学出版社"};
};
传递结构体变量
- 结构体变量之间可以通过赋值号传递信息,前提是变量的类型相同;
例子:
#include <stdio.h>int main(void)
{struct Test{int x;int y;}t1, t2;t1.x = 3;t1.y = 4;t2 = t1;//赋值号传递printf("t2.x = %d, t2.y = %d", t2.x, t2.y);return 0;
}
输出结果
t2.x = 3, t2.y = 4
结构体指针
例子:图书录入程序
#include <stdio.h>struct Date
{int year;int month;int day;
};struct Book
{char title[128];char author[40];float price;struct Date date;char publisher[40];
};struct Book getInput(struct Book book);
void printBook(struct Book book);struct Book getInput(struct Book book)
{printf("请输入书名: ");scanf("%s", book.title);printf("请输入作者: ");scanf("%s", book.author);printf("请输入售价: ");scanf("%f", &book.price);printf("请输入出版日期: ");scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);printf("请输入出版社: ");scanf("%s", book.publisher);return book;};void printBook(struct Book book)
{printf("书名: %s\n", book.title);printf("作者: %s\n", book.author);printf("售价: %.2f\n", book.price);printf("出版日期: %d-%d-%d\n", book.date.year,book.date.month,book.date.day);printf("出版社: %s\n", book.publisher);
}int main(void)
{struct Book b1, b2;printf("请录入第一本书的信息...\n");b1 = getInput(b1);putchar('\n');printf("请录入第二本书的信息...\n");b2 = getInput(b2);printf("\n\n录入完毕,现在开始打印验证...\n\n");printf("打印第一本书的信息..\n");printBook(b1);putchar('\n');printf("打印第二本书的信息..\n");printBook(b2);return 0;
}
输出结果
请录入第一本书的信息...
请输入书名: 老人与海
请输入作者: 海明威
请输入售价: 20
请输入出版日期: 2020-1-13
请输入出版社: 清华大学出版社请录入第二本书的信息...
请输入书名: 年轻人与海
请输入作者: 海明威
请输入售价: 20
请输入出版日期: 2020-1-13
请输入出版社: 北京大学出版社录入完毕,现在开始打印验证...打印第一本书的信息..
书名: 老人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 清华大学出版社打印第二本书的信息..
书名: 年轻人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 北京大学出版社
现在用结构体指针
还是上面的例子,略作修改
#include <stdio.h>struct Date
{int year;int month;int day;
};struct Book
{char title[128];char author[40];float price;struct Date date;char publisher[40];
};void getInput(struct Book *book);
void printBook(struct Book *book);void getInput(struct Book *book)
{printf("请输入书名: ");scanf("%s", book->title);printf("请输入作者: ");scanf("%s", book->author);printf("请输入售价: ");scanf("%f", &book->price);printf("请输入出版日期: ");scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);printf("请输入出版社: ");scanf("%s", book->publisher);
};void printBook(struct Book *book)
{printf("书名: %s\n", book->title);printf("作者: %s\n", book->author);printf("售价: %.2f\n", book->price);printf("出版日期: %d-%d-%d\n", book->date.year,book->date.month,book->date.day);printf("出版社: %s\n", book->publisher);
}int main(void)
{struct Book b1, b2;printf("请录入第一本书的信息...\n");getInput(&b1);putchar('\n');printf("请录入第二本书的信息...\n");getInput(&b2);printf("\n\n录入完毕,现在开始打印验证...\n\n");printf("打印第一本书的信息..\n");printBook(&b1);putchar('\n');printf("打印第二本书的信息..\n");printBook(&b2);return 0;
}
输出结果
打印第一本书的信息..
书名: 老人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 清华大学出版社打印第二本书的信息..
书名: 年轻人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 北京大学出版社
2020.1.13 C语言学习 结构体+结构体数组+结构体指针相关推荐
- C语言学习之将一个二维数组的行和列互换,存到另一个二维数组中
C语言学习之将一个二维数组的行和列互换,存到另一个二维数组中 #include <stdio.h> int main(){int a[2][3]={{1,2,3},{4,5,6}}; // ...
- C语言学习笔记之动态内存分配结构体——辣鸡版学生管理系统
@[C语言学习笔记] 小辣鸡自学C语言中,好多视频教程都有这个案例,小辣鸡自己边学边记,下边是代码. 如有错误,感谢告知!!! #include<stdio.h> #include< ...
- go var type 互转_Go语言学习笔记(第九章) 结构体
Go语言基础之结构体 Go语言中没有"类"的概念,也不支持"类"的继承等面向对象的概念.Go 通过类型别名(alias types)和结构体的形式支持用户自定义 ...
- c语言switch同时比较多个变量,C语言学习if和switch分支选择结构
如何判断一个整数是否为正数 计算机同我们一样,也是可以判断大小.在此之前,我们需要先说明在计算机中用来判断两个数关系的符号,即关系运算符. 关系运算符号 c语言中的关系运算符与数学上学习的有什么不同? ...
- go语言学习笔记(5)数组与切片
数组与切片 1.数组与切片的相同以及不同点 它们都属于集合类型,它们的值都可以用来存储某一种类型的值,但是数组类型的值的长度是固定的,而切片类型的值是可变长的.数组的长度在声明它的时候必须给定并且以后 ...
- c语言字符数组给字符指针,C语言常见有关问题之字符串数组和字符指针数组有关问题...
C语言常见问题之字符串数组和字符指针数组问题 通过如下的实例来讲解这个问题: #include int main(void) { char a[3][4] = {"123", &q ...
- 结构体怎么赋值_c语言学习之基础知识点介绍:结构体的介绍
一.结构体的介绍 /* 语法:struct 结构体名{成员列表;};切记切记有分号!说明:成员列表就是指你要保存哪些类型的数据.注意:上面的语法只是定义一个新的类型,而这个类型叫做结构体类型.因为类型 ...
- C语言学习笔记 (005) - 二维数组作为函数参数传递剖析
前言 很多文章不外乎告诉你下面这几种标准的形式,你如果按照它们来用,准没错: //对于一个2行13列int元素的二维数组 //函数f的形参形式 f(int daytab[2][13]) {...}// ...
- 翁恺老师C语言学习笔记(九)数组的运算
· 在一组给定的数据中,如何找出某个数据是否存在? 先看一段代码 #include<stdio.h> /* 找出key在数组a中的位置 @param key 要寻找的数字 @param a ...
最新文章
- 年薪40万,我用的是这套Python学习体系
- TCP/IP详解--第十八章
- 简单的JSP登录程序
- Unity3D学习笔记之二资源导入以及工程管理
- visual studio 正则表达式 查找与替换文本
- the android sdk location cannot be at the filesystem root
- 使用文件进行输入输出的两种方式(算法竞赛入门经典第2章)
- 关闭centos6.7不必要的服务
- 工厂物资管理E-R图
- Java订单接入支付宝二 支付回调
- Word怎么撤销到上一步操作
- SQL Server2019安装步骤(超详细 附下载链接)
- Ubuntu 18 安装截图工具 flameshot
- 三星手机大量死机!我反编译折腾半天后,发现竟然一个汉字引发的....
- 如何介绍自己测试过的项目
- CF545C Woodcutters
- 一起来找茬:下面这段代码是让计算机在屏幕上输出“hi”。其中有三个错误,快来改正吧
- 【Spark2运算效率】第四节 影响生产集群运算效率的原因之数据倾斜
- iOS-微信授权登录之时不能正常返回App
- 是否允许应用获取设备信息_手机权限获取弹窗不断,隐私安全如何确保,这些权限需谨慎...