POINTERS
A LECTURE FOR THE C++ COURSE
Each slide may have its own narration in an audio file. �For the explanation of any slide, click on the audio icon to start the narration.
The Professor‘s C++ Course by Linda W. Friedman is licensed under a �Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
ADDRESSES OF VARIABLES
variable name 🡪 & 🡪 variable address
POINTERS
2
WORKING WITH ADDRESSES
//ptr1.cpp
#include <iostream>
int main(){
int n=33;
cout << "n= " << n << endl;
cout << "&n= " << &n << endl;
return 0;
}
Output:
The address output is a hexadecimal number. All hex numbers start with 0x.
POINTERS
3
n= 33
&n= 0x0068fe00
WORKING WITH ADDRESSES
Aside:
Let’s convert the hex number to one in the base 10 number system that we are more familiar with.
0 0 6 8 f e 0 0
167 166 165 164 163 162 161 160
[=6 x (16)5 + 8 x (16)4 + 15 x (16)3 + 14 x (16)2 + 0 x (16)1 + 0 x (16)0
=6 x 1,048,576 + 8 x 65,536 + 15 x 4096 + 14 x 256 + 0 + 0]
So, 0068fe00(16) = 6880768(10)
POINTERS
4
n= 33
&n= 0x0068fe00
REFERENCE VARIABLES
//ptr2.cpp
#include <iostream>
int main(){
int n=33;
int &r = n;
cout << n << "\t\t" << r << endl;
n--;
cout << n << "\t\t" << r << endl;
r*= 2;
cout << n << "\t\t" << r << endl;
cout << &n << '\t' << &r << endl;
return 0;
}
POINTERS
5
REFERENCE VARIABLES
POINTERS
6
BACK TO POINTERS
What does this have to do with pointers? If we take the address of a variable and store it in another variable, that's a pointer.
p n
POINTERS
7
∙
33
THE POINTER ‘TYPE’
POINTERS
8
DEREFERENCING A POINTER
Since *p is an alias for n, we can use it to get the value of n. This is called dereferencing a pointer.
The & and * operators are inverses of each other.
n = = *p n = = *&n
p = = &n p = = &*p
We say that a (regular) variable directly references a value and a pointer indirectly references a value.
POINTERS
9
INITIALIZATION OF POINTERS
POINTERS
10
PASSING BY REFERENCE
POINTERS
11
PASSING BY REFERENCE
Same output, using pointer variables as parameters:
POINTERS
12
PASSING BY REFERENCE
Pointers can themselves be passed by reference, if we wish to change the addresses stored in them. If a pointer is not specifically passed by reference, the system will make a local copy of the pointer in the function, and any changes to the pointer are not carried back to the calling function.
POINTERS
13
LVALUES
const int MAX = 999; //OK
MAX = 27; //error
POINTERS
14
LVALUES
[Immutable lvalues: Arrays, functions, references. (later)]
type & refname = lvalue; For example,
POINTERS
15
CONSTANT VS NON-CONSTANT POINTERS
Examples:
POINTERS
16
CONSTANT VS NON-CONSTANT POINTERS
[from Hubbard, p. 169]
int n = 44; //n is an int
int * p = &n; // pointer to n
++(*p); //OK. Increments *p (an integer)
++p; //OK. Increments p (a pointer)
int * const cp = &n; //a const pointer to n
++(*cp); //OK. Increments *cp
++cp; // error. Pointer cp is a constant
const int k = 88; //k is a const integer
const int * pc = &k; //pc points to k
++(*pc); //error: *pc is a constant (k)
++pc; //OK. Increments pointer pc
const int * const cpc = &k; //const pointer to a const integer
++(*cpc); //error: *cpc is a const
++cpc; //error: pointer cpc is a constant
POINTERS
17
POINTER ARITHMETIC:
++ + +=
−− − −=
POINTERS
18
ARRAYS AND POINTERS
We can use pointer arithmetic to traverse an array:
POINTERS
19
ARRAYS AND POINTERS
Output:
POINTERS
20
ARRAYS AS PARAMETERS
We know that when we pass an array name as an argument to a function it is passed by reference even if there is no &. This is because the array name is a reference to the address of the first array element array[0]. In other words, it is a pointer. The compiler automatically converts int a[] in the argument list to int * const a. The default for an array name is as a constant pointer to non-constant data. This means that the pointer always points to the same memory location, and the data at that location can be modified through the pointer.
For example, the bubble sort algorithm using reference parameters:
POINTERS
21
ARRAYS AS PARAMETERS
Output:
POINTERS
22
POINTERS TO/FROM FUNCTIONS
POINTERS
23
POINTERS TO/FROM FUNCTIONS
This program will sum f(0) + f(2) + f(3) + … + f(n) for any function.
POINTERS
24
REVIEW
POINTERS
25
What did we learn in this lecture? Plenty.