
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to pass an entire structure as an argument to function in C?
There are three ways by which the values of structure can be transferred from one function to another. They are as follows −
Passing individual members as arguments to function.
Passing entire structure as an argument to function.
Passing the address of structure as an argument to function.
Now let’s see how to Pass an entire structure as an argument to function.
Name of the structure variable is given as argument in function call.
It is collected in another structure variable in function header.
The disadvantage is that a copy of an entire structure is created again by wasting the memory.
Example
The following program shows how to pass an entire structure as an argument to function.
#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
",d.day); printf("month=%s
",d.month); printf("year=%d
",d.year); }
Output
When the above program is executed, it produces the following result −
enter the day, month and year:18 JAN 2021 day=18 month=JAN year=2021
Example 2
Consider another example, wherein, a C program to demonstrate the passing of an entire structure as an argument to function is explained.
#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); }
Output
When the above program is executed, it produces the following result −
Enter variable 1 = 20 Enter variable 2 = 50 Added value is 70
Example 3
An another C program to demonstrate the passing of an entire structure as an argument to function is given herewith which explains declaring structure, declaring and returning function etc.
#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
",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); }
Output
When the above program is executed, it produces the following result −
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
- Related Articles
- How to pass entire structure as an argument to function in C language?
- How to pass entire array as an argument to a function in C language?
- How to pass the address of structure as an argument to function in C?
- How to pass the address of structure as an argument to function in C language?
- How to send an entire array as an argument in C language?
- How to pass individual elements in an array as argument to function in C language?
- How to pass Python function as a function argument?
- How to pass a dictionary as argument in Python function?
- How to pass argument to an Exception in Python?
- Java Program to Pass ArrayList as the function argument
- Can we pass objects as an argument in Java?
- How to pass individual members of structure as arguments to function in C language?
- How to pass an argument to the event handler in Tkinter?
- How to pass an object as a parameter in JavaScript function?
- How to pass the individual members as arguments to function using structure elements?
