如何在 C 语言中将结构体的各个成员作为参数传递给函数?

cserver side programmingprogramming更新于 2025/5/15 10:07:17

将结构体的各个成员作为参数传递给函数 −

  • 每个成员在函数调用中都作为参数传递。

  • 它们在函数头中被独立地存储在普通变量中。

示例

#include<stdio.h>
//声明结构体//
struct student{
   int s1,s2,s3;
}s[5];
//声明并返回函数//
void addition(int a,int b,int c){
   //声明 sum 变量和 For 循环变量//
   int i,sum;
   //算术运算//
   for(i=1;i<4;i++){
      sum=a+b+c;
      printf("Student %d scored total of %d
",i,sum);    } } void main(){    //声明For循环的变量//    int i;    //通过For循环读取用户I/P//    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);    }    //调用函数//    addition(s[].s1,s[].s2,s[].s3); }

输出

day = 2
month = 1
year = 2010

相关文章