Home > Backend Development > C++ > How to pass entire structure as parameter to function in C language?

How to pass entire structure as parameter to function in C language?

WBOY
Release: 2023-09-06 08:53:06
forward
1174 people have browsed it

How to pass entire structure as parameter to function in C language?

There are three ways to pass the value of a structure from one function to another. They look like this:

  • Pass a single member as a parameter to a function.

  • Pass the entire structure as a parameter to the function.

  • Pass the address of the structure as a parameter to the function.

Now let’s see how to pass the entire structure as a parameter to a function.

  • Give the names of structure variables as arguments in function calls.

  • Collect it into another structure variable in the function header.

  • The disadvantage is that it wastes memory and creates a copy of the entire structure again.

Example

The following program demonstrates how to pass an entire structure as a parameter to a function.

Demonstration

#include<stdio.h>
struct 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</p><p>",d.day);
   printf("month=%s</p><p>",d.month);
   printf("year=%d</p><p>",d.year);
}
Copy after login

Output

When the above program is executed, it produces the following results −

enter the day, month and year:18 JAN 2021
day=18
month=JAN
year=2021
Copy after login

Example 2

Consider another An example, in this case, explains how a C program can pass an entire structure as a parameter to a function.

Online Demonstration

#include<stdio.h>
//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);
}
Copy after login

Output

When the above program is executed, it produces the following results −

Enter variable 1 = 20
Enter variable 2 = 50
Added value is 70
Copy after login

Example 3

This is another C program that demonstrates passing an entire structure as a parameter to a function. It explains things like declaring structures, declaring and returning functions, etc.

Online Demonstration

#include<stdio.h>
//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</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);
}
Copy after login

Output

When the above program is executed, it produces the following results −

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

The above is the detailed content of How to pass entire structure as parameter to 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