How to pass the address of structure as an argument to function in C language?


Passing 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.

Advantages

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

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

Example

#include<stdio.h>
struct date{
   int day;
   int mon;
   int yr;
};
main (){
   struct date d= {02,01,2010};
   display (&d);
   getch ();
}
display (struct date *dt){
   printf("day = %d
", dt->day);    printf("month = %d
",dt->mon);    printf("Year = %d",dt->yr); }

Output

day = 2
month = 1
Year = 2010

Updated on: 09-Mar-2021

397 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements