当前位置:网站首页>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");
}
}边栏推荐
- 汉字风格迁移---结合本地和全局特征学习的中文字体迁移
- 响应式织梦模板塑身瑜伽类网站
- Scala basics [regular expressions, framework development principles]
- 初始 List 接口
- 栈的压入、弹出序列
- The principle and use of AOSP CameraLatencyHistogram
- The Chinese Valentine's Day event is romantically launched, don't let the Internet slow down and miss the dark time
- Use tf.image.resize() and tf.image.resize_with_pad() to resize images
- A simple understanding of TCP, learn how to shake hands, wave hands and various states
- Go编译原理系列7(Go源码调试)
猜你喜欢
随机推荐
HCIP BGP lab report
射频芯片ATE测试从入门到放弃之参数测试
Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
The principle and use of AOSP CameraLatencyHistogram
YOLOv7改进之二十二:涨点神器——引入递归门控卷积(gnConv)
【OpenCV图像处理】 图像拼接技术
rosbridge-WSL2 && carla-win11
Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
Shell 用法梳理总结
The salary of soft testers at each stage, come to Kangkang, how much can you get?
Unity2021发布WebGL雾效消失问题
Storage engine written by golang, based on b+ tree, mmap
leetcode/子串中不能有重复字符的最长子串
密码学基础以及完整加密通讯过程解析
一文搞定 SQL Server 执行计划
二叉搜索树解决落叶问题
逆波兰表达式求值
学习笔记 | uiautomation(如何)实现自动化
- the skip/skipif Pytest learning
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应









