Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer Basics Psst… over there.

Similar presentations


Presentation on theme: "Pointer Basics Psst… over there."— Presentation transcript:

1 Pointer Basics 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 Declaring a Pointer * in a declaration means "pointer to"
x can point to an integer value: y can point to a double value:

4 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

5 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

6 Initializing Pointers
Use address of a variable to set pointers Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04

7 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

8 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

9 Dereferenced Can use dereferenced address to get or set value Value
Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04

10 Dereferenced Can use dereferenced address to get or set value Value
Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04

11 Dereferenced Can use dereferenced address to get or set value Value
Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04

12 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

13 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

14 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 }

15 Syntax Issues

16 * Location These two both say x is a pointer to an int:
Type: Pointer to Int Name: X

17 * Location BUT, This says x stores a pointer to an int y stores an int

18 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

19 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

20 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

21 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

22 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

23 Read Backwards Const vars must be pointed to with const value pointers:


Download ppt "Pointer Basics Psst… over there."

Similar presentations


Ads by Google