Page 244 - DCAP404 _Object Oriented Programming
P. 244

Unit 11: Pointers and Dynamic Memory Management




                         Address of j  = 3276                                                   Notes
                         Address of k  = 7234
                         Value of j    = 6485
                         Value of k    = 3276

                         Value of i    = 3
                         Value of i    = 3
                         Value of i    = 3
                         Value of i    = 3





              Task  Explain the advantages of pointers to pointers.

          11.4 Pointer and Functions

          A function is a user defined operation that can be invoked by applying the call operator (“( )”) to
          the function’s name. If the function expects to receive  arguments, these arguments  (actual
          arguments) are placed inside the call operator. The arguments are separated by commas. A
          functions may be involved in one of the two way;
          1.   Call by value
          2.   Call by reference

          Call by Value

          In this method, the value of each actual argument in the calling function is copied on to the
          corresponding formal  arguments of the called function. The called function works with  and
          manipulates its own copy of arguments and because of this, changes made to the formal arguments
          in the called function have no effect on the values of the actual arguments in the calling function.

          Call by Reference

          This method can be used in two ways:
          1.   By passing the reference
          2.   By passing the pointer.
          A reference is an alias name for a variable.

          For instance, the following code
          float value = 1700.25;
          float &amt = value;   //amt reference of value

          declares amt as an alias name for the variable value. No separate memory is allocated for amt
          rather the variable value can now be accessed by two names value and amt.
                                                    value
                                       6009      1700.25
                                                     amt



                                           LOVELY PROFESSIONAL UNIVERSITY                                   237
   239   240   241   242   243   244   245   246   247   248   249