当前位置:网站首页>C 语言结构体数组指针以及函数
C 语言结构体数组指针以及函数
2022-07-17 05:20:00 【阿卡基YUAN】
结构体数组指针
指针变量可以指向一个结构体数组,这时指针变量的值是整个数组的首地址。
设 ps 为指向结构体数组的指针变量,则 ps 也指向该结构体数组的第 0个元素,ps+1
指向第一个元素,ps+i 则指向第 i 元素,这与普通数组的情况是一样的。
【示例】用指针变量输出结构体数组。
#include <stdio.h>
struct stu{
int num;
char *name;
char sex;
float score;
} *ps, boy[5]={
{101, "Zhou ping", 'M', 45},
{102, "Zhang ping", 'M', 62.5},
{103, "Liou fang", 'F', 92.5},
{104, "Cheng ling", 'F', 87},
{105, "Wang ming", 'M', 58}
};
int main(){
printf("No\tName\t\tSex\tScore\t\n");
for(ps=boy; ps<boy+5; ps++)
printf("%d\t%s\t%c\t%f\t\n", ps->num, ps->name, ps->sex,
ps->score);
return 0;
}
运行结果:
No Name Sex Score
101 Zhou ping M 45.000000
102 Zhang ping M 62.500000
103 Liou fang F 92.500000
104 Cheng ling F 87.000000
105 Wang ming M 58.000000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
应该注意的是,一个结构体指针变量虽然可以用来访问结构体变量或结构体数组元素的
成员,但是,不能使它指向一个成员。也就是说不允许取一个成员的地址来赋予它。所以,
下面的赋值是错误的:
ps=&boy[1].sex;
而只能是:
ps=boy; // 赋予数组首地址
或者是:
ps=&boy[0]; //赋予 0 号元素首地址
结构体指针变量作函数参数
结构体变量代表的是结构体本身这个整体,而不是首地址,作为函数参数时传递的整个
结构体,也就是所有成员。如果结构体成员较多,尤其是成员为数组时,传送的时间和空间
开销会很大,严重降低程序的效率。所以最好的办法就是使用指针,也就是用指针变量作为
函数参数。这时由实参传向形参的只是地址,非常快速。
【示例】计算一组学生的平均成绩和不及格人数。
#include <stdio.h>
#define STU struct stu
STU{
int num;
char *name;
char sex;
float score;
}boy[5]={
{101,"Li ping",'M',45},
{102,"Zhang ping",'M',62.5},
{103,"He fang",'F',92.5},
{104,"Cheng ling",'F',87},
{105,"Wang ming",'M',58}
};
void average(STU *ps);
int main(){
STU *ps = boy;
average(ps);
return 0;
}
void average(struct stu *ps){
int flunk=0, i;
float sum=0;
for(i=0; i<5; i++,ps++){
sum += ps->score;
if(ps->score < 60) flunk += 1;
}
printf("sum=%.2f, average=%.2f, flunk=%d\n", sum, sum/5, flunk);
}
运行结果:
sum=345.00, average=69.00, flunk=2
————————————————
版权声明:本文为CSDN博主「洛铭」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42528287/article/details/86617841
边栏推荐
- 机器学习篇-逻辑回归的分类预测
- F5 GTM(一):DNS参数
- Learning video saliency from human gaze using candidate selection
- Hand in hand building a home NAS universal server (1) | configuration selection and preparation
- Preparation of blast Library of rust language from scratch (2) -- detailed explanation of Blas matrix format
- Open source online markdown editor -- [editor.md]
- 实验二 类与对象定义初始化
- 感知智能手机上用户的关注状态
- 山西省第二届网络安全技能大赛(企业组)部分赛题WP(三)
- 视图、索引文件的应用
猜你喜欢
Read pictures and convert them to show different color spaces
[force buckle] realize stack with queue
Experiment 5: Gui
[jmeter] TCP Sampler
Handle Chinese word segmentation IK word segmenter and expand and stop dictionary
手把手搭建家用 NAS 全能服务器(1)| 配置选择及准备
[force buckle] bracket matching
Manjaro 系统日常使用入门导引
吴恩达机器学习第8-9章
Perception de l’état d’attention des utilisateurs sur les smartphones
随机推荐
实验三 继承性和派生类
Depth first search (DFS for short)
[Niuke] traversal of binary tree
Introduction to daily use of manjaro system
Handle Chinese word segmentation IK word segmenter and expand and stop dictionary
吴恩达机器学习第8-9章
Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (II)
Vcenter6.7安装及排错
《PyTorch深度学习实践》-B站 刘二大人-day6
[force buckle] maximum depth of binary tree
Automatic completion & (custom) Pinyin word Separator &
wireshark抓包:错误分析
[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....
实验四 运算符重载和虚函数
渣渣学习之路(1)输出某年某月的日历页
机器人缝合手势识别和分类
[Li Kou] a subtree of another tree
无80和443端口下申请域名SSL证书(适用于 acme.sh 和 certbot)
Perceive the attention status of users on smart phones
Manjaro 系统日常使用入门导引