Pointers 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
Pointers are the Force Pointers the glue that link portions of code Indirection / Sharing resources / Dynamic memory
Pointers are the Force Pointers have a dark side Can mess things up real good
Pointer Types Pointers can only point to a certain type of data Need to know how to interpret what we find if x is an int pointer we have issues… 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:
* 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
* Location BUT, This says Ways to make two pointers: x stores a pointer to an int y stores an int Ways to make two pointers:
Value Uninitialized pointers point to random location Value 100 Our name Memory location Value 0x00 0x01 100 p 0x02 0x38232 0x03 0x04
Getting Addresses &x 0x01 & : 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 memory locations Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04
Accessing Pointees * outside of declaration is dereference operator *p "the thing at the address stored in p" *p = 100 Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04
Dereferenced Use dereference to get / set values Value 100 0x00 x 0x01 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 }
Pointers and Functions Functions can take pointers: C equivalent to pass by ref #include <iostream> using namespace std; //C++ style void doubleVariable(int& x) { x = x * 2; } //C style void doubleVariableCStyle(int* x) { *x = (*x) * 2; } int main() { int num = 10; cout << num << endl; doubleVariable(num); cout << num << endl; doubleVariableCStyle(&num); cout << num <<#include <iostream> using namespace std; //C++ style void doubleVariable(int& x) { x = x * 2; } //C style void doubleVariableCStyle(int* x) { *x = (*x) * 2; int main() { int num = 10; cout << num << endl; doubleVariable(num); doubleVariableCStyle(&num); return 0; endl; return 0; }
Return Pointers Can return a pointer from a function: