CNSP - Lecture 10� Chapter 10
BY PROF. RAFAEL ORTA
Disclosure: this presentation includes slides that were provided by the textbook publisher and Dr. Hnatyshin, some are adaptation, and some are an exact replica.
Last week we covered
9.1 Introduction to Search Algorithms
9.2 Searching an Array of Objects
9.3 Introduction to Sorting Algorithms
9.4 Sorting an Array of Objects
9.5 Sorting and Searching Vectors
9.6 Introduction to Analysis of Algorithms
This week we will cover
10.1 Pointers and the Address Operator
10.2 Pointer Variables
10.3 The Relationship Between Arrays
and Pointers
10.4 Pointer Arithmetic
10.5 Initializing Pointers
10.6 Comparing Pointers
10.7 Pointers as Function Parameters
10.8 Pointers to Constants and Constant Pointers
10.9 Dynamic Memory Allocation
10.10 Returning Pointers from Functions
How do you call individual components of a struct?
A/ Members
10.1 Pointers and the Address Operator
int num = -23;
cout << # // prints address
// in hexadecimal
10.2 Pointer Variables 1 of 3
Pointer Variables 2 of 3
int *intptr;
“intptr can hold the address of an int” or “intptr is a pointer to an int”
int * intptr;
int* intptr;
Pointer Variables 3 of 3
int num = 25;
int *intptr;
intptr = #
cout << intptr; // prints 0x4a00
cout << *intptr; // prints 25
*intptr = 20; // puts 20 in num
10.3 The Relationship Between Arrays �and Pointers 1 of 2
An array name is the starting address of the array
int vals[] = {4, 7, 11};
cout << vals; // displays 0x4a00
cout << vals[0]; // displays 4
The Relationship Between Arrays and �Pointers 2 of 2
int vals[] = {4, 7, 11};
cout << *vals; // displays 4
int *valptr = vals;
cout << valptr[1]; // displays 7
Pointers in Expressions
int vals[]={4,7,11};
int *valptr = vals;
cout << *(valptr+1); // displays 7
cout << *(valptr+2); // displays 11
Array Access 1 of 2
Array elements can be accessed in many ways
Array access method | Example |
array name and [ ] | vals[2] = 17; |
pointer to array and [ ] | valptr[2] = 17; |
array name and subscript arithmetic | *(vals+2) = 17; |
pointer to array and subscript arithmetic | *(valptr+2) = 17; |
Array Access 2 of 2
vals[i]
is equivalent to the pointer notation
*(vals + i)
10.4 Pointer Arithmetic 1 of 5
Some arithmetic operators can be used with pointers:
Pointer Arithmetic 2 of 5
Assume the variable definitions
int vals[]={4,7,11};
int *valptr = vals;
Examples of use of ++ and --
valptr++; // points at 7
valptr--; // now points at 4
Pointer Arithmetic 3 of 5
Assume the variable definitions:
int vals[]={4,7,11};
int *valptr = vals;
Example of the use of + to add an int to a pointer:
cout << *(valptr + 2)
This statement will print 11
Pointer Arithmetic 4 of 5
Assume the variable definitions:
int vals[]={4,7,11};
int *valptr = vals;
Example of use of +=:
valptr = vals; // points at 4
valptr += 2; // points at 11
Pointer Arithmetic 5 of 5
Assume the variable definitions
int vals[] = {4,7,11};
int *valptr = vals;
Example of pointer subtraction
valptr += 2;
cout << valptr - val;
This statement prints 2: the number of
ints between valptr and val
10.5 Initializing Pointers
int *ptr = NULL;
int num, *numPtr = #
int val[ISIZE], *valptr = val;
float cost;
int *ptr = &cost; // won't work
Initializing Values in C++ 11
int *ptr = nullptr;
int *ptr{ };
10.6 Comparing Pointers
if (ptr1 == ptr2) // compares
// addresses
if (*ptr1 == *ptr2) // compares
// contents
10.7 Pointers as Function Parameters 1 of 3
Pointers as Function Parameters 2 of 3
Requires:
1) asterisk * on parameter in prototype and
header
void getNum(int *ptr);
2) asterisk * in body to dereference the pointer
cin >> *ptr;
3) address as argument to the function in the call
getNum(&num);
Pointers as Function Parameters 3 of 3
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int num1 = 2, num2 = -3;
swap(&num1, &num2); //call
Passing an Array Via a Pointer �Parameter
10.8 Pointers to Constants and Constant Pointers
Pointers to Constants
const double taxRates[] =
{0.65, 0.8, 0.75};
const double *ratePtr;
Pointer to Constant – What does the Definition Mean?
Read as: “rates is a pointer to a constant that is a double.”
Constant Pointers
int classSize = 24;
int * const classPtr = &classSize;
Constant Pointer – What does the Definition Mean?
Read as: “ptr is a constant pointer to an int.”
Constant Pointer to Constant
int size = 10;
const int * const ptr = &size;
10.9 Dynamic Memory Allocation 1 of 2
double *dptr;
dptr = new double;
Dynamic Memory Allocation 2 of 2
arrayPtr = new double[25];
Dynamic Memory Example
int *count, *arrayptr;
count = new int;
cout <<"How many students? ";
cin >> *count;
arrayptr = new int[*count];
for (int i=0; i<*count; i++)
{
cout << "Enter score " << i << ": ";
cin >> arrayptr[i];
}
Releasing Dynamic Memory
delete count;
delete [] arrayptr;
Dangling Pointers and Memory Leaks
10.10 Returning Pointers from Functions
int* newNum();
More on Memory Leaks
General guidelines to avoid memory leaks: