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

CServer Side ProgrammingProgramming

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\n", dt->day);
   printf("month = %d\n",dt->mon);
   printf("Year = %d",dt->yr);
}

Output

day = 2
month = 1
Year = 2010
raja
Published on 09-Mar-2021 08:04:37
Advertisements