Passing Struct through functions

“Passing structure through function” statement has 2 meanings: Passing structure variable through function, passing structure member through function. We are going to understand both one by one.

Passing structure variable through function

In this, we pass the variable of structure as an argument of a function. Syntax of the same is listed below for function declaration, call and definition.

Function declaration
<return type>  <function name>(<structure name with keyword “struct”>);
Function call
<function name>(<structure variable name>);
Function definition
<return type>  <function name>(<structure name with keyword “struct”>  <any name to structure variable>)

{

//code for function

}
Example program of passing structure variable through function

In the below example program, there is a structure named str and its variable is str1. As per our purpose, we want to pass the structure variable as a function argument. Which is str1 here.
In the function argument, we have to write data type. So here we have to write a data type of str1 so we are writing struct str as the data type of str1. Because struct str is a data type of variable which we are passing through a function. The function “fun” prints values of all members.

#include<stdio.h>
#include<conio.h>
struct str{		
int a;
char b;
}str1={1,'c'};

void fun(struct str); //function argument
void main()
{
  clrscr();
  fun(str1);
  getch();
}

void fun(struct str str1)
{
  printf("str1 : a=%d , b=%c ",str1.a,str1.b);
}

Output:

Passing structure members through function

In this, we pass the variable of structure with member as an argument of a function. Syntax of the same is listed below for function declaration, call and definition.

Function declaration
<return type>  <function name>(<structure member data type>);
Function call
<function name>(<structure variable name>.<structure member name>);
Function definition
<return type>  <function name>(<structure member data type>  <any name to that structure member>);

{

//code for function

}
Example program of passing structure member through function

In the below example program, there is a structure named str and its variable is str1 as the previous example. This structure is having a member: A. As per our purpose, we want to pass structure member as a function argument. Which is A here. In the function argument, we have to write data type.
So here we have to write data type A so we are writing int. Because int is a data type of variable/ member which we are passing through function. The function is printing the value of structure member “A”.

#include<stdio.h>
#include<conio.h>
struct str{		
int A;
}str1={1};
void fun(int);
void main()
{
  clrscr();
  fun(str1.A);
  getch();
}
void fun(int A)
{
  printf("str1 : A=%d ",A);
}

Output:

Leaning from this blog:

  1. Difference between “Passing structure variable through function” and “Passing structure member through function”.
  2. Syntax of function declaration/ call/ definition for “Passing structure variable through function”.
  3. Syntax of function declaration/ call/ definition for “Passing structure member through function”.
  4. How to write code for both.
  5. Need of using both “Passing structure variable through function” and “Passing structure member through function”.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments