# How to pass the address of 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 us understand how to pass the address of structure as an argument to function.

• The address of the structure is passed as an argument to the function.

• It is collected in a pointer to structure in function header.

The advantages of passing the address of structure as an argument to function are as follows −

• No wastage of memory as there is no need of creating a copy again.

• No need of returning the values, as the function can access indirectly the entire structure and then work on it.

## Example

The following program shows how to pass the address of a structure as an argument to function −

Live Demo

#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);
return 0;
}
void display(struct date *p){
printf("day=%d",p->day);
printf("month=%s",p->month);
printf("year=%d",p->year);
}

## Output

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

enter the day, month and year:20 MAR 2021
day=20
month=MAR
year=2021

## Example

Given below is a C program to demonstrate the structures and functions by calling the entire function as an argument. Due to this method of calling function, there is no wastage of memory, since we don't need to copy again and return values.

Live Demo

#include<stdio.h>
//Declaring structure//
struct student{
char Name[100];
int Age;
float Level;
char temp;
}s[5];
//Declaring and returning Function//
void show(struct student *p){
//Declaring variable for For loop within the function//
int i;
//For loop for printing O/p//
for(i=1;i<3;i++){
printf("The Name of student %d is : %s",i,p->Name);
printf("The Age of student %d is : %d",i,p->Age);
printf("The Level of student %d is : %f",i,p->Level);
p++;
}
}
void main(){
//Declaring variable for for loop//
int i;
//Declaring structure with pointer//
struct student *p;
for(i=0;i<2;i++){
printf("Enter the Name of student %d : ",i+1);
gets(s[i].Name);
printf("Enter the Age of student %d : ",i+1);
scanf("%d",&s[i].Age);
printf("Enter the Level of student %d :",i+1);
scanf("%f",&s[i].Level);
scanf("%c",&s[i].temp);//Clearing Buffer//
printf("Enter the Grade of student %d :",i+1);
}
//Assigning pointer to structure//
p=&s;
//Calling function//
show(&s);
}

## Output

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

Enter the Name of student 1 : Lucky
Enter the Age of student 1 : 27
Enter the Level of student 1 :2
Enter the Grade of student 1 :A
Enter the Name of student 2 : Pinky
Enter the Age of student 2 : 29
Enter the Level of student 2 :1
Enter the Grade of student 2 :B
The Name of student 1 is : Lucky
The Age of student 1 is : 27
The Level of student 1 is : 2.000000
The Grade of student 1 is : A
The Name of student 2 is : Pinky
The Age of student 2 is : 29
The Level of student 2 is : 1.000000
The Grade of student 2 is : B