# Passing Structures to Functions

## Passing Structures to Functions

We can pass structute by two ways :
1. Passing Structure Members to Functions
2. Passing Entire Structures to Functions

## Passing Structure Members to Functions

When you pass a member of a structure to a function, you are passing the value of that member to the function. It is irrelevant that the value is obtained from a member of a structure.

struct fred
{
char x;
int y;
float z;
char s[10];
} mike;

Here each member being passed to a function :
func(mike.x);                 /* passes character value of x */
func2(mike.y);                /* passes integer value of y */
func3(mike.z);                /* passes float value of z */
func4(mike.s);                /* passes address of string s */
func(mike.s[2]);                /* passes character value of s[2] */

In each case, it is the value of a specific element that is passed to the function. It does not matter that the element is part of a larger unit.
The & operator precedes the structure name, not the individual member name. Note also that s already signifies an address, so no & is required.

## Passing Entire Structures to Functions

When a structure is used as an argument to a function, the entire structure is passed using the normal call-by-value method. Of course, this means that any changes made to the contents of the parameter inside the function do not affect the structure passed as the argument.

When using a structure as a parameter, remember that the type of the argument must match the type of the parameter. For example, in the following program both the argument arg and the parameter parm are declared as the same type of structure.

## A Program to illustrates Passing Entire Structures to Functions.

\* C Program to illustrates Passing Entire Structures to Functions. *\

# include < stdio.h >
struct   struct_type
{

int   a, b  ;
char   ch  ;

} ;
void f1( struct struct_type parm ) ;
int   main( )
{

struct struct_type arg ;
arg.a = 1000 ;
f1 ( arg ) ;
return ( 0 ) ;

}
void f1( struct struct_type parm
{

printf(" %d ",  parm.a) ;

} ;

As this program illustrates, if you will be declaring parameters that are structures, you must make the declaration of the structure type global so that all parts of your program can use it. For example, had struct_type been declared inside main( ), it would not have been visible to f1( ).