Home >Backend Development >C++ >How to pass the address of a structure as a parameter to a function in C language?

How to pass the address of a structure as a parameter to a function in C language?

PHPz
PHPzforward
2023-08-30 23:29:06804browse

How to pass the address of a structure as a parameter to a function in C language?

Pass the address of the structure to the function as a parameter −

  • Pass the address of the structure to the function as a parameter.

  • Collect it into the structure pointer in the function header.

Advantages

  • No memory is wasted because there is no need to create a copy

  • No need to Value is returned because the function can indirectly access the entire structure and operate 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</p><p>", dt->day);
   printf("month = %d</p><p>",dt->mon);
   printf("Year = %d",dt->yr);
}

Output

day = 2
month = 1
Year = 2010

The above is the detailed content of How to pass the address of a structure as a parameter to a function in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete