首頁  >  文章  >  後端開發  >  如何在C語言中將結構體的各個成員作為參數傳遞給函數?

如何在C語言中將結構體的各個成員作為參數傳遞給函數?

PHPz
PHPz轉載
2023-09-21 20:57:031380瀏覽

如何在C語言中將結構體的各個成員作為參數傳遞給函數?

將各個成員作為參數傳遞給函數 -

  • #每個成員都作為函數呼叫中的參數傳遞。

  • 它們在函數頭中的普通變數中獨立收集。

範例

#include<stdio.h>
//Declaring structure//
struct student{
   int s1,s2,s3;
}s[5];
//Declaring and returning Function//
void addition(int a,int b,int c){
   //Declaring sum variable and For loop variable//
   int i,sum;
   //Arithmetic Operation//
   for(i=1;i<4;i++){
      sum=a+b+c;
      printf("Student %d scored total of %d</p><p>",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[].s1,s[].s2,s[].s3);
}

輸出

day = 2
month = 1
year = 2010

以上是如何在C語言中將結構體的各個成員作為參數傳遞給函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除