6.5.structures and Functions
6.5.structures and Functions
For structures to be fully useful, we must have a mechanism to pass them to functions and
return them. A function may access the members of a structure in three ways as shown in
figure below.
Output:
2. PASSING THE ENTIRE STRUCTURE:
The second method involves passing of a copy of the entire structure to the called
function.
Since the function is working on a copy of the structure, any changes to structure
members within the function are not reflected in the original structure(in the calling
function).
It is , therefore, necessary for the function to return the entire structure back to the
calling function .
All compilers may not support this method of passing the entire structure as a
parameter.
The general format of sending a copy of a structure to the called function is:
function_name(structure_variable_name);
Program :
OUTPUT:
3. PASSING THE ADDRESS OF THE STRUCTURE:
The third approach employs a concept called pointers to pass the structure as an argument. In
this case, the address location of the structure is passed to the called function. The function can
access indirectly the entire structure and work on it.
function_name(&structure_variable_name);
Program:
Output: