Structure pointer variable passed as function parameter

Publisher:WhisperingLightLatest update time:2022-12-12 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Structural variables can be passed in their entirety as function parameters. Transfer process: All members are transferred one by one. Pointer variables are passed as function parameters. Then only the address is passed from the actual parameter to the formal parameter, thereby reducing the time and space overhead.


Programming using structure pointer variables as function parameters.

#include


#define STU struct stu


void ave(struct stu *ps);


STU //stu structure name

{

int num;

char *name;

char sex;

float score;

}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},

}; //boy[5] is a structure array, which is of type stu. Assign an initial value to the structure array.




int main()

{

struct stu *ps; //ps structure pointer variable points to the first address of the boy[] structure array.

ps=boy;

ave(ps); //ps is used as an actual parameter to call the function ave to complete the calculation.

}

void ave(struct stu *ps)

{

int c=0,i;

float ave,s=0; //s total score, ave average score

for(i=0;i<5;i++,ps++) //ps++ cannot be dropped, otherwise it will loop 5 times in boy[1].

{

s+=ps->score;

if(ps->score<60)

c+=1; //cThe number of people who failed

}

printf("s=%fn",s);

ave=s/5;

printf("average=%fncount=%dn",ave,c);

}


The formal parameter is the pointer variable ps, boy is defined as an external structure array, and the entire source program is valid.


Reference address:Structure pointer variable passed as function parameter

Previous article:What should I do if the microcontroller clock is inaccurate? Adjust this way!
Next article:7 common interface types in circuit design

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号