Page 77 - PowerPoint Presentation
P. 77

CAVITE STATE UNIVERSITY
                               T3 CAMPUS
                               Department of Information Technology      DCIT 25 – Data Structures and Algorithms











                               Memory allocation before incrementing ptSudentNumber1

                       What  would  be  the  value  stored  in  the  pointer  variable  studentNumber1  if  the
               studentNumber1 is incremented by 1 using the following statement?
                                                ptStudentNumber1++;

                       This is tricky because the value of ptStudentNumber1 is 0. If you increment it by
               one, the new value is 1. However, memory address 2 is the second half of the memory location
               reserved for studentNumber1.This means that ptStudentNumber1 would point to the middle
               of the values of studentNumber1, which doesn’t make sense.
                       That’s  not  what  happens.  The  computer  uses  pointer  arithmetic.  Values  are
               incremented and decremented in pointer arithmetic using the size of a data type. That is, if the
               memory address contains an integer and the memory address is incremented, the computer
               adds the size of an integer to the current memory address.

               Pointer to Pointers
                       Imagine having a list of a million students along with their final grades and student
               numbers  and  being  asked  to  sort  the  list  by  last  name,  first  name,  and  student  number.
               Intuitively, you might think about making two copies of this list, each placed in one of the sort
               orders. However, this wastes memory. There is a better approach to sort the list: use pointers
               to pointers.
                       You learned that a pointer is a variable that contains the memory address of another
               variable. A pointer to a pointer is also a variable that contains the memory address, except a
               pointer to a pointer contains the memory address of another pointer variable.
                       Let’s begin by declaring four  char variables and initializing them with letters of the
               alphabet. This is shown in the first statement of the following example. The second statement
               declares a pointer called ptInitial and a pointer to a pointer called ptPtInitial. A pointer is
               declared using a signal asterisk (*). A pointer is declared using a double asterisk (**).

                             char initial1 = ‘D’, initial2 = ‘A’, initial3 = ‘C’, initial4 = ‘B’;

                             char *ptInitial, **ptPtInitial;
                             ptInitial = &initial1;

                             ptPtInitial = &ptInitial;

                       With variables declared, the next two statements assign values to the pointer and to
               the  pointer  to  a  pointer.  In  both  cases,  the  ampersand  (&)  is  used  as  the  dereferencing
               operator.
                       The ptInitial pointer variable is assigned the address of variable initial1, which is
               memory  address 1.  The  ptPtInitial pointer  to  a  pointer  variable  is  assigned the  memory
               address of ptInitial. The address of ptInitial is memory address 5.





                                                                                                 Page | 24
   72   73   74   75   76   77   78   79   80   81   82