Page 240 - DCAP404 _Object Oriented Programming
P. 240

Unit 11: Pointers and Dynamic Memory Management




          pointer, de-references the pointer’s value. When you see * operator used, think of the phrase  Notes
          “value pointed to”.

               !

             Caution  The * operator retrieves the value that is “pointer to” by variable it proceeds.

          Self Assessment

          Fill in the blanks:
          1.   A pointer is a variable that is used to store a …………………….. address.
          2.   There are two important pointer operators such as ‘*’ and ……………….. .

          3.   The ‘*’ operator is called the ……………………. operator.
          4.   Every variable is located under unique  location within a computer’s memory and this
               unique location has its own …………………… address, the memory address.

          11.2 Declaring Pointers


          A pointer is a special variable that return the address of a memory. If you need to declare a
          variable to hold your age, you might use the following variable declaration:
          int age = 18 // declares to variable to hold my age

          Declaring age this way does several things. Because C++ knows that you need a variable called
          age, C++ reserves storage for that variable. C++ also knows that you will store only integers in
          age, not floating-point or double floating point data. You also have requested that C++ store the
          value of 18 in age after reserving storage for that variable.
          Suppose that you want to declare a pointer variable, not to hold your age but to point to age, the
          variable that hold your age. p_age might be a good name for this pointer variable. Figure given
          below shows an illustration of what you want to do. This example assumes that C++ store age at
          address 1002, although your C++ compiler arbitrarily determines the address of age, and it
          could be anything.

                   Figure 11.1:  Page Contains the  Address  of  Age; p_age  to the  Age  Variable

                    Address                    Memory

                    Age at 350,606             30             p_age “points to age”
                                               .
                                               .
                                               .
                    p_age at 356, 982          350606

                    Top of memory

          To declare the p_age pointer variable. You should do the following:
          int*_age; // declare an integer pointer
          As with the declaration for age, this line reserves a variable called p_age. It is not a normal
          integer variable, however, because of the C++ knows that this variable is to be a pointer variable.



                                           LOVELY PROFESSIONAL UNIVERSITY                                   233
   235   236   237   238   239   240   241   242   243   244   245