Skip to main content

Passing structure to a Function

     Like an ordinary variable, a structure variable can also be passed to a function. We  may either pass individual structure elements or the entire structure. Let us take an example in which there is structure employee that has name, sex and salary as members. These members can be passed to function to display their value.
   Example:
               In this example, member variables are displayed by passing individual elements to a function display().
void display(char name[],char s, int sal)
  {
   printf("\nName=%s\tSex=%c\tSalary=%d",name,s,sal);
  }

void main()
  {
  struct employee
     {
    char name[20];
    char sex;
    int salary;
   };
  struct employee e={"abc",'M',10000};
  display(e.name,e.sex,e.salary);
                                      /*function call in which each elements are passed individually*/
  getch();


  }
Another Example:
        Let us consider above structure but pass whole structure variable to function.

struct employee
   {
    char name[20];
    char sex;
    int salary;
   };   // structure is declared outside main()

void display(struct employee emp)
  {
   printf("\nName=%s\tSex=%c\tSalary=%d",emp.name,emp.sex,emp.salary);
  }

void main()
  {
  struct employee e={"abc",'M',10000};
  clrscr();
  display(e);  /* function call*/
  getch();
  }

In above example, the whole structure variable e is passed to a function display(). In formal argument of function display(), we need structure variable to receive structure variable sent by main() function. Thus, we should declare structure outside the main() function such that it is recognized also by function display().

Comments

Popular posts from this blog

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...

Explanation of Graphics in C programming

   The header file graphics.h should be included to use graphics related built in functions. Again, the variable gDriver is an integer variable that specifies graphics driver to be used. Device drivers are small programs which talk directly to the hardware. Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. The initialization of this variable to DETECT is to set another variable gMode to the highest resolution available for the detected driver.  The value of this variable gDriver may be another than DETECT (e.g. 1 for CGA, 9 for VGA.). The variable gMode is an integer that specifies initial graphics mode.       The built in function initgraph() is for graphics initialization. The syntax for this is as   initgraph(&graphics_driver, &graphics_mode, path_to_driver);   path_to_driver specifies the directory path where initgraph() looks for graphics drivers ( .BGI files). Thi...

BIOS

BIOS stands for Basic Input Output System. It is is kind of OS pre- attached in the system Design to detect every I/O devices It holds basic drivers -checks all I/O devices -Loads main OS -Responsible for starting System switched on manufactured by AMI,Phonix,award,Baiostar POST error test, boots trap load, BIOS is unintrusive system. CMOS and BIOS are two different components of the motherboard. CMOS work as storage if BIOS. A basic software program containing all BIOS is permanently stored in the ROM. every essencial holds program Tresh passing without knowinig is called intrusive.