Right Click to Search

Thursday, March 11, 2025

Passing Structure to Function in C Programming



Passing Structure to Function in C Programming

  1. Structure can be passed to function as a Parameter.
  2. function can also Structure as return type.
  3. Structure can be passed as follow
Example :
#include<stdio.h>
#include<conio.h>
//-------------------------------------
struct Example
{
  int num1;
  int num2;
}s[3];
//-------------------------------------
void accept(struct Example *sptr)
{
  printf("\nEnter num1 : ");
  scanf("%d",&sptr->num1);
  printf("\nEnter num2 : ");
  scanf("%d",&sptr->num2);
}
//-------------------------------------
void print(struct Example *sptr)
{
  printf("\nNum1 : %d",sptr->num1);
  printf("\nNum2 : %d",sptr->num2);
}
//-------------------------------------
void main()
{
int i;
clrscr();
for(i=0;i<3;i++)
 accept(&s[i]);
for(i=0;i<3;i++)
 print(&s[i]);
getch();
}
Output :
Enter num1 : 10
Enter num2 : 20
Enter num1 : 30
Enter num2 : 40
Enter num1 : 50
Enter num2 : 60
Num1 : 10
Num2 : 20
Num1 : 30
Num2 : 40
Num1 : 50
Num2 : 60

Bookmark & Share

Tags / Keywords : | , ,

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates