当前位置:网站首页>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");
}
}边栏推荐
- With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
- The Chinese Valentine's Day event is romantically launched, don't let the Internet slow down and miss the dark time
- Pytest学习-setup/teardown
- HCIP BGP lab report
- End-to-End Lane Marker Detection via Row-wise Classification
- Unity2021发布WebGL雾效消失问题
- JS获得URL超链接的参数值
- Pytest learn-setup/teardown
- 用两个栈模拟队列
- 设置工作模式与环境(下):探查和收集信息
猜你喜欢

电子邮件安全或面临新威胁!

Three.js入门详解

BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记

智能座舱的「交互设计」大战

- the skip/skipif Pytest learning

Deep integration of OPC UA and IEC61499 (1)

代码随想录笔记_动态规划_416分割等和子集

Unity intercepts 3D images and the implementation of picture-in-picture PIP

数据分析知识点搜集(纯粹的搜集)

用两个栈模拟队列
随机推荐
Graph-node:创建一个新的subgraph
(PC+WAP)织梦模板螺钉手柄类网站
超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
rosbridge-WSL2 && carla-win11
跨域的学习
Kotlin - 扩展函数和运算符重载
libnet
HCIP BGP lab report
3D Semantic Segmentation - 2DPASS
libnet
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
Creo9.0 绘制中心线
Jmeter-断言
【RYU】rest_router.py源码解析
禾匠编译错误记录
BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes
逆波兰表达式求值
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
Kotlin - extension functions and operator overloading
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例