Pointer Basics Psst… over there
Pointers Pointer : data type with value that is a memory address x is a pointer Stores location to find value Our name Memory location Value 0x00 0x01 12.5 x 0x02 0x03 0x04
Declaring a Pointer * in a declaration means "pointer to" x can point to an integer value: y can point to a double value:
Value Uninitialized pointers point to random location What value does p point to? Our name Memory location Value 0x00 0x01 100 p 0x02 0x38232 0x03 0x04
Getting Addresses & is address of operator &x "Give me the memory address of x" &x 0x01 Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x38232 0x03 0x04
Initializing Pointers Use address of a variable to set pointers Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04
Using Pointers Can work directly with pointers Value = copies <, > == compare the memory locations Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04
Accessing Pointees * is the dereference operator *p says Value Outside of variable declarations *p says "Give me the value at the address in p" Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04
Dereferenced Can use dereferenced address to get or set value Value Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04
Dereferenced Can use dereferenced address to get or set value Value Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04
Dereferenced Can use dereferenced address to get or set value Value Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04
Pointer to Pointer Can point to a pointer Value Address of the address of a value Two dereferences to access Our name Memory location Value 0x00 i 0x01 200 p 0x02 0x1 q 0x03 0x2 0x04 int i = 5; int* p = &i; int** q = &p; cout << q << endl ; //2 cout << *q << endl ; //1 cout << **q << endl ; //200
NULL Pointers NULL means nothing C C++ NULL pointer points to nothing NULL is keyword that has value 0 C++ NULL usually works, but supposed to use nullptr or 0
Using NULL NULL or 0 used to safely indicate pointer does not have a pointee p = NULL; //C style p = 0; //older C++ p = nullptr; //modern C++ if(p) { //only get here if not null/0 }
Syntax Issues
* Location These two both say x is a pointer to an int: Type: Pointer to Int Name: X
* Location BUT, This says x stores a pointer to an int y stores an int
Typedef Typedef can eliminate some syntax issues int* x, y; //x is a pointer to an int, y is an int int *x, *y; //x and y both pointers to ints typedef int* intPtr; //intPtr means int* intPtr x, y; //x and y both pointers to ints
Const & Pointers Pointer's value (address) can be constant Can't change where it points Values they point to can be constant Can't change the value of whatever it points to
Read Backwards p1 is a constant pointer to an int Can't point at a new location Value we are pointing to can be changed
Read Backwards p1 is a pointer to an int that is constant Can point at a new location Value we are pointing to cannot be changed
Read Backwards p1 is a constant pointer to an int that is constant Cannot point at a new location Value we are pointing to cannot be changed
Read Backwards Const vars must be pointed to with const value pointers: