Pointer Assignments As with any variable you may use a pointer variable on the

Pointer assignments as with any variable you may use

This preview shows page 10 - 18 out of 47 pages.

Pointer Assignments As with any variable, you may use a pointer variable on the right-hand side of an assignment statement to assign its value to another pointer variable #include <iostream> using namespace std; int main () { int x; int * p1, * p2; p1 = & x; // Address of x is assigned to p1 // Content of p1 (which is the address of x) // is assigned to p2 p2 = p1; cout << "The address of x: " << p2 << endl; return 0 ; } Rm 3553, [email protected] COMP2012 (Fall 2019) 10 / 47
Image of page 10
11 / 47
Image of page 11
Pointer Arithmetic ONLY TWO arithmetic operations are available on pointers. They are I Addition I Subtraction Therefore, C++ supports four operators for pointer arithmetic operations, that are +, - , ++ and -- To understand what occurs in pointer arithmetic, let p1 be an integer pointer with current value of 2000. Also, assume integers are 4 bytes long, after the expression p1++ , I p1 contains 2004, NOT 2001 The same is true of decrements. For example, assuming that p1 has the value 2000, the expression p1-- results I p1 has the value 1996 Rm 3553, [email protected] COMP2012 (Fall 2019) 12 / 47
Image of page 12
Pointer Arithmetic Graphical representation of last example Rm 3553, [email protected] COMP2012 (Fall 2019) 13 / 47
Image of page 13
Pointer Arithmetic Generalizing from preceding example, the following rules govern pointer arithmetic I Each time a pointer is incremented , it points to the memory location of the next element of its base type I Each time a pointer is decremented , it points to the location of the previous element I When applied to character pointers, this will appear as ” normal arithmetic because characters are always 1 byte long I All other pointers will increase or decrease by the length of the data type they point to Rm 3553, [email protected] COMP2012 (Fall 2019) 14 / 47
Image of page 14
Pointer Arithmetic (Cont’d) You are not limited to the increment and decrement operators For example, you may add or subtract integers to or from pointers I The expression p1 = p1 + 2; makes p1 point to the second element of p1’s type beyond the one it currently points to I The expression p1 = p1 - 2; makes p1 points to the second element of p1’s type precede the one it is currently points to Rm 3553, [email protected] COMP2012 (Fall 2019) 15 / 47
Image of page 15
Pointer Arithmetic Graphical representation of the last example Rm 3553, [email protected] COMP2012 (Fall 2019) 16 / 47
Image of page 16
Pointer Comparisons We can compare two pointers in a relational expression For instance, given two pointers (i.e., pointer variables), p and q , the following statements are perfectly valid I if(p < q) cout << "p points to lower memory than q" << endl; I if(p > q) cout << "p points to higher memory than q" << endl; I if(p == q) cout << "p points to the same memory as q" << endl; Generally, pointer comparisons are used when two or more pointers point to a common objects Rm 3553, [email protected] COMP2012 (Fall 2019) 17 / 47
Image of page 17
Image of page 18

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture