当前位置:网站首页>C language structure array pointer and function
C language structure array pointer and function
2022-07-19 06:47:00 【Akaki yuan】
Structure array pointer
Pointer variables can point to an array of structures , At this time, the value of the pointer variable is the first address of the entire array .
set up ps Is a pointer variable to the structure array , be ps It also points to the second row of the structure array 0 Elements ,ps+1
Point to first element ,ps+i Then point to the second i Elements , This is the same as the case of ordinary arrays .
【 Example 】 Output structure array with pointer variable .
#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;
}
Running results :
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
It should be noted that , Although a structure pointer variable can be used to access structure variables or structure array elements
member , however , You can't make it point to a member . In other words, it is not allowed to take a member's address to give it . therefore ,
The following assignment is wrong :
ps=&boy[1].sex;
And can only be :
ps=boy; // Give the first address of the array
Or is it :
ps=&boy[0]; // give 0 First address of element No
Structure pointer variables as function parameters
The structure variable represents the whole structure itself , Not the first address , The entire... Passed as a function parameter
Structure , That is, all the members . If the structure has more members , Especially when the members are arrays , Time and space of transmission
It's going to cost a lot , Seriously reduce the efficiency of the program . So the best way is to use pointers , That is, use pointer variables as
Function parameter . At this time, only the address is passed from the argument to the formal parameter , Very fast .
【 Example 】 Calculate the average score of a group of students and the number of failed students .
#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);
}
Running results :
sum=345.00, average=69.00, flunk=2
————————————————
Copyright notice : This paper is about CSDN Blogger 「 LuoMing 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_42528287/article/details/86617841
边栏推荐
- 網絡中的一些基本概念
- Get the first and last values after Oracle grouping and sorting
- Wu Enda machine learning chapter 12-13
- Experiment 5: Gui
- Handle Chinese word segmentation IK word segmenter and expand and stop dictionary
- Cygwin 配合 Listary 切换当前目录快速打开
- 从输入URL到展示出页面
- Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (III)
- X11 forwarding
- 渣渣学习之路(1)输出某年某月的日历页
猜你喜欢

Pytorch deep learning practice-b station Liu erden-day5

mass data

Face recognition error

How can the new generation of CS sdnand (also known as patch T card) make the old MCU youthful

Visual saliency based visual gaze estimation

Introduction to daily use of manjaro system

Information on successful cooperation between CS brand sdnand and stm32mcu

吴恩达机器学习第6-7章

数据库的查询(二)

实验五: GUI
随机推荐
Drawing PCB with Altium Designer
渣渣学习之路(2)纯小白向:Win Server 2003服务器搭建
用Altium Designer绘制PCB图
Face recognition error
Restclient query document
吴恩达机器学习第12-13章
Solution: unable to load file c:\program files\ Because running scripts is forbidden on this system
通过数据加密方案阻止勒索攻击
Get the first and last values after Oracle grouping and sorting
吴恩达机器学习第3-4章
机器学习篇-逻辑回归的分类预测
二分查找及其引申
DSL realizes automatic completion query
DSL implements bucket aggregation
Pytorch deep learning practice-b station Liu erden-day3
NAND nor flash flash flash product overview
Manjaro 系统日常使用入门导引
Seachest utilities tool enables your hard disk to consume less power and live longer
Full experience of soft examination at the beginning, middle and advanced levels
oracle中merge into 与update的性能对比