Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-08-30 23:29:06
forward
736 people have browsed it

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);
}
Copy after login

Output

day = 2
month = 1
Year = 2010
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!