当前位置:网站首页>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");
}
}
边栏推荐
- In V8 how arrays (with source code, picture and text easier to understand)
- log4j-slf4j-impl cannot be present with log4j-to-slf4j
- jav一键生成数据库文档
- 逆波兰表达式求值
- Binary search tree to solve the fallen leaves problem
- 2021年数据泄露成本报告解读
- Unity 截取3D图像 与 画中画PIP的实现
- 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
- 3D 语义分割——2DPASS
- (PC+WAP)织梦模板螺钉手柄类网站
猜你喜欢
智能管理PoE交换机
vscode插件设置——Golang开发环境配置
Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
Binary search tree to solve the fallen leaves problem
单例模式使用饿汉式和懒汉式创建一定安全?很多人不知
Minimized installation of debian11
【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
FinClip,助长智能电视更多想象空间
随机推荐
Creo 9.0二维草图的诊断:着色封闭环
射频芯片ATE测试从入门到放弃之参数测试
【OpenCV图像处理】 图像拼接技术
SRE运维解密-什么是SRE:DevOps模型的具体实践!
HNUCM 2022年暑假ACM搜索专项练习
The "interaction design" battle of the smart cockpit
[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
The longest substring that cannot have repeating characters in a leetcode/substring
跨域的学习
BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes
The super perfect layout has shortcut keys and background replacement
rsync basic usage
LeetCode 0155. 最小栈
Pytest学习-setup/teardown
libnet
代码重构:面向单元测试
最小化安装debian11
Shell 用法梳理总结
密码学基础以及完整加密通讯过程解析
逆波兰表达式求值