0% found this document useful (0 votes)
1 views3 pages

6.5.structures and Functions

The document explains three methods for passing structures to functions in programming: passing individual members, passing the entire structure, and passing the address of the structure. Each method has its implications, such as whether changes to the structure are reflected in the original or if pointers are used for indirect access. The document provides general formats and examples for each method.

Uploaded by

bitravenubitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

6.5.structures and Functions

The document explains three methods for passing structures to functions in programming: passing individual members, passing the entire structure, and passing the address of the structure. Each method has its implications, such as whether changes to the structure are reflected in the original or if pointers are used for indirect access. The document provides general formats and examples for each method.

Uploaded by

bitravenubitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Passing individual members

Passing structures to functions Passing the entire structure

Passing the address of the structure

1.Passing individual members:


 To pass any individual member of the structure to a function, we must use the direct
selection operator to refer to the individual members for the actual parameters.
 The called program does not know if the two variables are ordinary variables or
structure members.
 Look at the following code that illustrates this concept.

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);

The called function takes the following form:


data_type function_name(struct_type st_name)
{
........
........
return(expression);
}

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.

The general format of sending address of a structure.

function_name(&structure_variable_name);

The called function takes the following form:


data_type function_name(struct_type *st_name)
{
..........
..........
}

Program:

Output:

You might also like