Home > Backend Development > C++ > body text

How to pass various members of a structure as parameters to a function in C language?

PHPz
Release: 2023-09-21 20:57:03
forward
1426 people have browsed it

How to pass various members of a structure as parameters to a function in C language?

Pass individual members as arguments to functions -

  • Each member is passed as an argument in a function call.

  • They are collected independently in ordinary variables in the function header.

Example

#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);
}
Copy after login

Output

day = 2
month = 1
year = 2010
Copy after login

The above is the detailed content of How to pass various members of a structure as parameters to a function in C language?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template