Page 74 - DCAP407_DATA_STRUCTURE
P. 74
Unit 4: Pointers
2. After executing the statements:
m = 10
n = 20
The values 10 and 20 will be copied into the memory locations recognized by
m and n in the addresses 1004 and 1008.
3. Function num() is invoked with two parameters m and n. Here, the addresses
of m and n are passed as parameters.
4. Control is transferred to the called function num(). Memory is allocated for the
formal parameters x and y. Let the addresses of the formal parameters be 2004
and 2008. Since, the address of m which is 1004 and address of n which is 1008
are passed as parameters, these addresses are copied into the location 2004
identified by x and 2008 identified by y. Here, the formal parameters x and y
contain the address of the actual parameters m and n, which means that the
values of actual parameters is accessed by the formal parameters through
pointers.
5. When statements
*x = 100
*y = 200
are executed, the memory location pointed to by x i.e., contents pointing to
1004 which is 10, is replaced by 100. Similarly, the memory location pointed by
y i.e., the contents pointing to 1008 which is 20 is replaced by 200.
6. Control is transferred from the called function to the calling function to the
point from where it was called.
7. The actual parameters are indirectly changed by the formal parameters.
8. Execution stops.
Hence, the values in one function can be changed by some other functions by
passing addresses, and then de-referencing the addresses using the indirection
operator (*) in the called function.
Advantages of Pass by Reference or Call by Reference
1. In pass by reference, information can be sent back to the calling function through
parameters.
2. A two-way transfer of information can be achieved through pass by reference.
Write a program to accept X elements into an array and then compute the sum using
pointers.
LOVELY PROFESSIONAL UNIVERSITY 67