Passing structure as parameter C

  • Individual structure members.
  • Structure as a whole.
  • Pointer to structure.

Structure can be passed to a function as parameter and can also be returned from functions using three approaches. Either individual members of structure, whole structure or pointer to structure can be passed.

Individual structure members
Since individual structure members can be accessed and act as individual entity, they can be passed and returned from functions.
Following example illustrates passing and return of individual member of structure.

Program demonstrating passing and return of individual member of structure

smith 2 22000.250000
In the declaration of structures, ‘name’ is declared as an array. Since we are passing the base address of name, the value is stored in emp_no and salary. it is a combination of call by reference as well as call by value. The salary is incremented by 2000 and returned to function main. In main it is assigned to e1. salary.

Thus it is seen that passing individual members to and from functions is analogous to ordinary variables.

Structures as a whole
When number of members of a structure is more and we need to pass all of them, individual member approach becomes tedious and lengthy. Instead we can pass structure as a whole. The following program illustrates passing entire structure to function and what is the effect of it.

/* program demonstrating passing entire structure to function.
structure not returned from function *//

smith 2 200000.250000
smith 2 200000.250000

The function ‘change’ accepts structure as an argument and assigns different values to each member of structure. Function returns nothing. In main, structure is initialized to some value. Value of each member is printed before calling function change and after calling function change. There is no difference between values before and after call to main. It thus shows that transfer is by value.

One more thing to be noted is that here structure format is defined outside main. If it is defined in main like previous example, function ‘change’ would not know what is struct emp. Hence its definition is made global. Thus while passing structure as a whole, it is necessary to make the structure definition known to the called as well as calling program.

Since structure can be returned from a function, (as against arrays which cannot be returned from a function) we can modify the program so that altered structure is returned from ‘change’ to main.

/* Modified to return structure from called function */

Smith 2 20000.25
Jones 16 9999.99
Thus the alterations made in function change are now available to main.

Passing pointer to structure
When large structures are to be passed to functions it is better to pass pointers to structures. Also passing pointer to structure makes altered values available to calling program. So we don’t need to return structure as in previous example.

If we write the previous program by passing pointer to struct emp

/* Rewritten using pointer to structure, pointer to structure is passed instead of whole
structure */

We can also return pointer to a structure from a function. This is mainly useful when several structures are passed to function but only one is returned.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics