B baden cse 100 lec 2 spring 2013 a b 19 making

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ters point •  The “address-of” prefix operator & applied to a variable returns the memory address of the variable int a=5, b= -999; int* pt1 = &a; // initialize pt1 to point to a address memory cell 512000 5 512004 -999 512008 identifier 512000 Scott B. Baden / CSE 100, Lec 2 / Spring 2013 a b pt1 20 An abstract picture of memory •  A useful abstract way to draw the contents of memory after those statements execute is shown here. The exact value of the address in the pointer is not shown. Instead, the fact that the pointer named pt1 points to the variable named a is shown with an arrow: int a=5, b= -999; int* pt1 = &a; // initialize pt1 to point to a a: 5 b: -999 pt1: •  Note: this picture is impossible in Java, which does not allow pointers to point directly to primitive type variables Scott B. Baden / CSE 100, Lec 2 / Spring 2013 21 Dereferencing pointers •  The “dereferencing” prefix operator * applied to a pointer value “goes to” the location in memory pointed to by the pointer int a=5, b= -999; int* pt1 = &a; // declare and initialize pt1 to point to a cout << *pt1; // print the value of what pt1 points to:5 *pt1 = 10; // assign a value to what pt1 points to cout << a; // print the value of a, which is... •  In C++ you can have pointers to anything! For example you can create a pointer to a pointer to an int: int ** pp = &pt1; // declare and initialize pp to point to pt1 **pp = 20; // assign a value to what pp points to points to...! cout << a // print the value of a, which is... Scott B. Baden / CSE 100, Lec 2 / Spring 2013 22 Pointer arithmetic in C++ •  These arithmetic operators are defined on pointer arguments: ++, --, +, -, +=, -= •  A pointer variable stores an integer number, which is a memory address •  However, arithmetic operations on a pointer variable do not necessarily change that number in the way you would expect •  For example, if p is a pointer, p++ does not necessarily make the number stored in p one larger than it was before •  ... it makes the number stored in p larger by an amount equal to sizeof(*p) Scott B. Baden / CSE 100, Lec 2 / Spring 2013 23 Pointer arithmetic, cont’d •  Following our example... int a=5, b= -999; int* pt1 = &a; // pt1 contains address 512000 pt1++; // now pt1 contains address 512004... why? *pt1 = 10; // assign a value to what pt1 points to cout << b; // print the value of b, which is... pt1 = pt1 + 1; // now pt1 contains address 512008 cout << *pt1; // print the value at this address pt1 = pt1 - 2; // now pt1 contains address 512000 again Scott B. Baden / CSE 100, Lec 2 / Spring 2013 24 Pointer arithmetic (II) •  When you add an integer to a pointer in C++, the result is of the same type as the pointer, and the increment is the integer times the size of the object the pointer is declared to point to •  When you subtract one pointer from another in C++, the result is of integer...
View Full Document

Ask a homework question - tutors are online