Page 73 - PowerPoint Presentation
P. 73

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

               Week 5: The Point of Pointers
               Objectives: After the completion of the chapter, students will be able to:
                     Learn the concept of Pointers
                     Assign an Address to a Pointer
                     Perform Pointer to Pointers

               Pointers
                       Whenever you reference the name of a variable, the name of an element of a structure,
               or  the  name  of  an  attribute  of  a  class,  you  are  telling  the  computer  that  you  want  to  do
               something with the contents stored at the corresponding memory location.
                       In the first statement in the following example, the computer is told to store the letter A
               into  the  memory  location  represented  by  the  variable  grade.  The  last  statement  tells  the
               computer to copy the contents of the memory location represented by the grade variable and
               store in in the memory location represented by the oldGrade variable.
                                            char grade = ‘A’
                                            char oldGrade;
                                            oldGrade = grade;

                       A pointer is a variable and can be used as an element of a structure and as an attribute
               of a class in some programming languages such as C++, but not Java. However, the contents
               of a pointer is a memory address of another location of memory, which is usually the memory
               address of another variable, element of a structure, or attribute of a class.

               Declaring a Pointer
                       A pointer is declared similar to how you declare a variable but with a slight twist. The
               following example declares a pointer called ptGrade. There are four parts of this declaration.
                     Data Type – The data type of the memory address stored in the pointer.
                     Asterisk (*) – Tells the computer that you are declaring a pointer.
                     Variable Name – The name that uniquely identifies the pointer and is used to reference
                       the pointer within your program.
                     Semicolon – Tells the computer this is an instruction (statement)
                                                     char *pgrade;

               Data Types and Pointers
                       As you will recall, a data type tells the computer the amount of memory to reserve and
               the kind of data that will be stored at the memory location. However, the data type of a pointer
               tells the computer something different. It tells the computer the data type of the value at the
               memory location whose address is contained in the pointer.
                       The  Asterisk  (*)  used  when  declaring  a  pointer  tells  the  computer  the  amount  of
               memory to reserve and the kind of data that will be stored at that location. That is, the memory
               size is sufficient to hold a memory address, and the kind of data stored there is a memory
               address.
                       The following example declares four variables. The first statement declares an integer
               variable  called  studentNumber.  The  second  statement  declares  a  char  variable  called
               grade. The last two statements each declare a pointer. The figure depicts memory reserve by
               these statements. Assume that a memory address is 4 bytes for this example:

                                            int studentNumber
                                            char grade;
                                            char *ptGrade;

                                            int *ptStudentNumber;
                       The  char data  type  in the ptGrade pointer  declaration tells the  computer  that  the
               address that will be stored in ptGrade is the address of a character. The contents of the
               memory location associated with ptGrade will contain the address of the grade variable.


                                                                                                 Page | 20
   68   69   70   71   72   73   74   75   76   77   78