Download presentation
Presentation is loading. Please wait.
1
Pointers Psst… over there
2
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
3
Pointers are the Force Pointers the glue that link portions of code
Indirection / Sharing resources / Dynamic memory
4
Pointers are the Force Pointers have a dark side
Can mess things up real good
5
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
6
Declaring a Pointer * in a declaration means "pointer to"
x can point to an integer value: y can point to a double value:
7
* Location These two both say x is a pointer to an int:
Type: Pointer to Int Name: X
8
* Location BUT, This says x stores a pointer to an int y stores an int
9
* 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:
10
Value Uninitialized pointers point to random location Value 100
Our name Memory location Value 0x00 0x01 100 p 0x02 0x38232 0x03 0x04
11
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
12
Initializing Pointers
Use address of a variable to set pointers Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04
13
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
14
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
15
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
16
Dereferenced Can use dereferenced address to get or set value Value
Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04
17
Dereferenced Can use dereferenced address to get or set value Value
Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04
18
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
19
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
20
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 }
21
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; }
22
Return Pointers Can return a pointer from a function:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.