C – Function with Structures

After reading this function with structures topic, you will understand its theory and examples also you will able to implement it in C programming.

In C function with structures, structure members can be passed to or from a function using the following methods:

  • Passing individual element of structure to function
  • Passing complete structure to function

Passing individual member of structure to function

During function call, individual structure member can be passed to a function as arguments, and a single structure member can be returned by the return statement. The example below will show the way of writing programs based on passing individual member of structure to function in C programming.

#include<stdio.h>
#include<conio.h>
void main(void)
{
struct language
{
char name;
int year;
};
void display(char,int);
struct language dr={'C',1972};
display(dr.name,dr.year);
getch();
}
void display(char k,int m)
{
printf("%c %d\n",k,m);
}

Output (1)

C 1972

Passing complete structure to function

In this method entire structure variable pass to a function at a time. The example below will show the way of writing programs based on passing the complete structure to function in C programming.

#include<stdio.h>
#include<conio.h>
struct language
{
char name;
int year;
};
void main(void)
{
void display(struct language);
struct language dr={'C',1972};
display(dr);
getch();
}
void display(struct language dr)
{
printf("%c %d\n",dr.name,dr.year);
}

Output (1)

C 1972

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *