How is structure passing and returning implemented?

Questions by pbchaudhari   answers by pbchaudhari

Showing Answers 1 - 6 of 6 Answers

baseersd

  • Jan 16th, 2008
 

Here i am trying to
i)accept input through GetEmployeeDetails()
ii)Print the same details through PrintEmployeeDetails()
iii)Modify / Change the details through ModifyEmployeeDetails()
iv)Print the modified values using PrintEmployeeDetails()


#include<stdio.h>

struct Employee
{
       int ID;
       char *name;       
};
void GetEmployeeDetails(struct Employee *Emp)
{
       printf("Enter the ID");
       scanf("%d",&Emp->ID);
       Emp->name = (char*)malloc(20 * sizeof(char));
       fflush(stdin);
       printf("nEnter the Name");
       gets(Emp->name);
}
void PrintEmployeeDetails(struct Employee Emp)
{
     printf("nEmployee ID   :: %d",Emp.ID);
     printf("nEmployee Name :: %snn",Emp.name);
}

struct Employee* ModifyEmployeeDetails(struct Employee Emp)
{
       printf("Enter the ID");
       scanf("%d",&Emp.ID);
       Emp.name = (char*)malloc(20 * sizeof(char));
       fflush(stdin);
       printf("nEnter the Name");
       gets(Emp.name);
       return &Emp;       
}
int main()
{
          struct Employee Emp1,*Emp2;
          GetEmployeeDetails(&Emp1);
          PrintEmployeeDetails(Emp1);
         Emp2 = ModifyEmployeeDetails(Emp1);
          PrintEmployeeDetails(*Emp2);
        system("pause");
        
}

  Was this answer useful?  Yes

Here is my code fragement. It shows passing the structure as argument and returning it as well

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>

struct abc {
        int x;
        float x1;
        char x2;
};

struct abc check(struct abc a2)
{
 printf("%dn",a2.x);
 printf("%fn",a2.x1);
 printf("%cn",a2.x2);
 a2.x=15;
 a2.x1=11.14;
 a2.x2='B';
 return a2;
}

int main()
{
  struct abc a1,a2;
  a1.x=10;
  a1.x1=5.7;
  a1.x2='A';
  a2=check(a1);
  printf("n");
  printf("%dn",a2.x);
  printf("%fn",a2.x1);
  printf("%cn",a2.x2);

  return 0;
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions