
- 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 entire structure as an argument to function in C language?
Passing 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.
Disadvantage
A copy of the entire structure is created again wasting memory
Program
Following program demonstrates passing an entire structure as an argument to function −
#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
Enter variable 1 = 30 Enter variable 2 = 40 Added value is 70
- Related Articles
- How to pass an entire structure as an argument to function in C?
- 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 language?
- How to pass the address of structure as an argument to function in C?
- 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 individual members of structure as arguments 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 send individual elements as an argument in C language?
- Java Program to Pass ArrayList as the function argument
- How to pass argument to an Exception in Python?
- How to pass the individual members as arguments to function using structure elements?
- Can we pass objects as an argument in Java?
- How to pass an argument to the event handler in Tkinter?

Advertisements