当前位置:网站首页>C语言实验十四 结构体
C语言实验十四 结构体
2022-08-03 23:37:00 【Meteor.792】
一、实验目的
1、掌握结构体类型变量的定义和使用;
2、掌握结构体类型数组的概念和使用;
二、实验内容
结构体
C 语言提供了一种如果用简单变量来分别代表属性,难以反映出他们之间的内在联系数据类型称为结构体。如学生姓名、编号、性别、年龄、各科成绩、 地址等。 他们是同一个处理对象,学生的属性,在这之间,即有字符型、也有长整、短整型、实型等各 种数据类型。例:
Num | name | sex | age | score | addr |
10010 | Li fum | m | 18 | 88.5 | beijin |
整型 | 字符型 | 字符 | 整型 | 实型 | 字符型 |
struct student
{ int num;
char name[20];
char sex;
short int age; float score; char addr[30];
}
#include "stdio.h"
void main()
struct student
{ { int num;
char name[20];
char sex;
short int age;
float score;
char addr[30];
} a={10010,"Li fum",'m',18,88.5,"Bei jing"};
printf("num:%d\nname:%s\nsex:%c\nage:%d\nscore:%f\naddr:%s\n",a.num,a.name,a.sex,a.age,a.score,a.addr);
}上面就定义了一个结构体类型, struct 是关键字,结构体类型是 student 。其中有 6 个不同的数据项。
结构体类型不同于基本数据类型的特点: (1)由若干个数据项组成,每个数据项称为一个结构体的成员,也可称为“域”。 (2)结构体类型并非只能有一种,而可以有千千万万。
struct 结构体名
{
成员项表列
};
定义一个结构体类型,并不意味着系统将分配一段内存单元来存放各数据项成员。 因为这仅仅只定义了类型。结构体类型需用户自己定义。
下面是结构体的应用(输出学生的学号、姓名和分数):
#include "stdio.h"
#define N 5
struct student
{
char num[6];
char name[8];
int score[4];
}stu[N];
void main()
{
int i,j;
void print(struct student stu[N]);
for(i=0;i<N;i++)
{
printf("\n输出学生的分数为:%d\n",i+1);
printf("学号:");scanf("%s",stu[i].num);
printf("名字:");scanf("%s",stu[i].name);
for(j=1;j<4;j++)
{
printf("分数%d:",j);
scanf("%d",&stu[i].score[j]);
}
printf("\n");
}
print(stu);
}
void print(struct student stu[N])
{
int i,j ;
printf("\n NO. name score1 score2 score3\n");
for(i=0;i<N;i++)
{
printf("%5s%1 0s",stu[i] .num,stu[i] .name);
for(j=1;j<=3;j++)
printf("%9d",stu[i] .score[j]);
printf("\n");
}
}边栏推荐
- Super perfect version of the layout have shortcut, background replacement (solve the problem of opencv Chinese path)
- AOSP CameraLatencyHistogram的原理与使用
- 【RYU】rest_router.py源码解析
- 【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
- 七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
- 电子邮件安全或面临新威胁!
- Click the icon in Canvas App to generate PDF and save it to Dataverse
- Scala basics [regular expressions, framework development principles]
- JS get parameter value of URL hyperlink
- YOLOv7改进之二十二:涨点神器——引入递归门控卷积(gnConv)
猜你喜欢
随机推荐
牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
The world's first mass production, with the most fixed points!How does this AVP Tier1 lead?
rosbridge-WSL2 && carla-win11
【LeetCode】最长公共子序列(动态规划)
HNUCM 2022年暑假ACM搜索专项练习
Super perfect version of the layout have shortcut, background replacement (solve the problem of opencv Chinese path)
The "interaction design" battle of the smart cockpit
SolidEdge ST8安装教程
Jmeter-参数化
Software testing is seriously involution, how to improve your competitiveness?
OpenCV 图像拼接
RSS feeds WeChat public - feed43 asain
End-to-End Lane Marker Detection via Row-wise Classification
详谈RDMA技术原理和三种实现方式
Creo 9.0创建几何点
重新认识浏览器的渲染过程
[RYU] rest_router.py source code analysis
Pytest学习-skip/skipif
用两个栈模拟队列
数据分析知识点搜集(纯粹的搜集)









