review-pointers-4page.pdf - What are Pointers A pointer or pointer variable is a variable that holds a memory address of another object(typically

review-pointers-4page.pdf - What are Pointers A pointer or...

This preview shows page 1 - 4 out of 12 pages.

COMP2012 Object-Oriented Programming and Data Structures Review: Pointers Dr. Desmond Tsoi Department of Computer Science & Engineering The Hong Kong University of Science and Technology Hong Kong SAR, China Rm 3553, [email protected] COMP2012 (Spring 2019) 1 / 47 What are Pointers? Rm 3553, [email protected] COMP2012 (Spring 2019) 2 / 47 Declaration of Pointer Variables If a variable is going to hold an address of another variable , it must be declared as follows: Actually, we can treat < type > * as a special type which is pointer type Rm 3553, [email protected] COMP2012 (Spring 2019) 3 / 47 Recall the syntax for declaring a pointer variable: < type > * < variable name > ; Examples: // Declare a pointer that points to a variable of integer type int * a; // the value of a is garbage but is not nullptr // Declare a pointer that points to a variable of double type int * b; // the value of b is garbage but is not nullptr // Declare a pointer that points to a variable of char type char * c; // the value of c is garbage but is not nullptr // It is no difference for you to put * close to type OR // close to name int * d; // the value of d is garbage but is not nullptr int * d; // same as above, no difference We will talk a bit more about nullptr later! Rm 3553, [email protected] COMP2012 (Spring 2019) 4 / 47
Image of page 1
Pointer Operator & (Address-Of) There are two operators associated with pointers. They are & and * (Note: The * here doesn’t mean multiplication ) The first operator, & is a unary operator (i.e. with single operand) that returns the memory address of another variable Usage: & < variable name > We can think of & as returning ”the address of” int var1 = 5 ; // pint receives the address of var1 int * pint = & var1; double var2 = 1.23 ; // pdouble receives the address of var2 double * pdouble = & var2; Rm 3553, [email protected] COMP2012 (Spring 2019) 5 / 47 Pointer Operator & (Address-Of)(Cont’d) Graphical representation of last example Rm 3553, [email protected] COMP2012 (Spring 2019) 6 / 47 Example - & (Address-Of) #include <iostream> using namespace std; int main () { int a, b; a = 88 ; b = 100 ; cout << "The address of a is " << & a << endl; cout << "The address of b is " << & b << endl; return 0 ; } Output: The address of a is 0x22ff74 The address of b is 0x22ff70 Rm 3553, [email protected] COMP2012 (Spring 2019) 7 / 47 Pointer Operator* (Dereference) The second operator, *, is the complement of operator & It is also a unary operator but accesses the value located at the address that it points to We can think of * as ” at address int var1 = 5 ; int * pint = & var1; // var2 receives the value at address pint int var2 = * pint; // Change the value of the memory location // pointed by pint to 10, therefore var1 = 10 as well * pint = 10 ; Rm 3553, [email protected] COMP2012 (Spring 2019) 8 / 47
Image of page 2
The Different Uses of Operator * Do not confuse the use of operator * in declaring a pointer variable versus the use of * as the dereference operator Example // This means to declare a pointer // variable // * that sticks with a data type refers // to a pointer variable declaration int * p; int i, j = 10 ; p = & j; // This means to dereference the variable p // * that sticks with a pointer variable // refers to dereference i = * p; Rm 3553, [email protected]
Image of page 3
Image of page 4

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture