如何在C语言中将整个结构作为参数传递给函数?

如何在c语言中将整个结构作为参数传递给函数?

有三种方法可以将结构的值从一个函数传递到另一个函数。它们如下所示:

将单个成员作为参数传递给函数。

将整个结构作为参数传递给函数。

将结构的地址作为参数传递给函数。

立即学习“C语言免费学习笔记(深入)”;

现在让我们看看如何将整个结构作为参数传递给函数。

在函数调用中给出结构变量的名称作为参数。

在函数头中将其收集到另一个结构变量中。

缺点是浪费内存,会再次创建整个结构的副本。

示例

以下程序演示了如何将整个结构作为参数传递给函数。

演示

#includestruct date{   int day;   char month[10];   int year;};int main(){   struct date d;   printf("enter the day,month and year:");   scanf("%d%s%d",&d.day,d.month,&d.year);   display(d);//passing entire structure as an argument to function   return 0;}void display(struct date d){   printf("day=%d

",d.day); printf("month=%s

",d.month); printf("year=%d

",d.year);}

输出

当上述程序被执行时,它产生以下结果 −

enter the day, month and year:18 JAN 2021day=18month=JANyear=2021

Example 2

考虑另一个例子,在这个例子中,解释了一个C程序如何将整个结构体作为参数传递给函数。

 在线演示

#include//Declaring structure//struct add{   int var1;   int var2;}a;//Declaring and returning Function//void show(struct add a){   //Declaring sum variable//   int sum;   //Arithmetic Operation//   sum=a.var1+a.var2;   //Printing O/p//   printf("Added value is %d",sum);}void main(){   //Declaring structure//   struct add a;   //Reading User I/p//   printf("Enter variable 1 = ");   scanf("%d",&a.var1);   printf("Enter variable 2 = ");   scanf("%d",&a.var2);   //Calling function//   show(a);}

输出

当上述程序被执行时,它产生以下结果 −

Enter variable 1 = 20Enter variable 2 = 50Added value is 70

示例3

这是另一个C程序,演示了将整个结构作为参数传递给函数的方法,其中解释了声明结构、声明和返回函数等内容。

 在线演示

#include//Declaring structure//struct student{   int s1,s2,s3;}s[5];//Declaring and returning Function//void addition(struct student s[]){   //Declaring sum variable and For loop variable//   int i,sum;   //Arithmetic Operation//   for(i=1;i<4;i++){      sum=s[i].s1+s[i].s2+s[i].s3;      printf("Student %d scored total of %d

",i,sum); }}void main(){ //Declaring variable for For loop// int i; //Reading User I/p through For loop// for(i=1;i<4;i++){ printf("Enter marks for student %d in subject 1 = ",i); scanf("%d",&s[i].s1); printf("Enter marks for student %d in subject 2 = ",i); scanf("%d",&s[i].s2); printf("Enter marks for student %d in subject 3 = ",i); scanf("%d",&s[i].s3); } //Calling function// addition(s);}

输出

当上述程序被执行时,它产生以下结果 −

Enter marks for student 1 in subject 1 = 25Enter marks for student 1 in subject 2 = 89Enter marks for student 1 in subject 3 = 45Enter marks for student 2 in subject 1 = 12Enter marks for student 2 in subject 2 = 45Enter marks for student 2 in subject 3 = 89Enter marks for student 3 in subject 1 = 12Enter marks for student 3 in subject 2 = 78Enter marks for student 3 in subject 3 = 12Student 1 scored total of 159Student 2 scored total of 146Student 3 scored total of 102

以上就是如何在C语言中将整个结构作为参数传递给函数?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:34:50
下一篇 2025年12月17日 21:35:03

相关推荐

发表回复

登录后才能评论
关注微信