Post on 19-Mar-2023
Pointer Variables
Last Lecture § What is a pointer?
§ A pointer “points” to another variable § It is the address/location of that variable in memory
§ We have seen pointers in our discussion of call-by-reference parameters § A call-by-reference parameter is an example of a
pointer to a variable
© J.S. Bradbury
void bubbleSort(string& s1);
CSCI 1060U Lecture 9 Slide 1
Pointer Variables
Last Lecture § A pointer is declared using an asterisk (*)
§ Each pointer can point to a variable of only one type § Example: *p2 can point to a variable of type int but
not a variable of type double.
© J.S. Bradbury
double *p1; int *p2; float *p3, *p4;
CSCI 1060U Lecture 9 Slide 2
The Dereferencing Operator and the Address-of Operator
Last Lecture § The asterisk (*) before the name of a pointer
variable is referred to as the dereferencing operator § The ampersand (&) is used in from of a non-pointer
variable (e.g., int or float variable) to get the variable’s address
§ We can use the & to allow a pointer variable to point to an already declared variable.
© J.S. Bradbury CSCI 1060U Lecture 9 Slide 3
Defined Pointer Types
Last Lecture § Instead of always using the asterisk (*) for pointer
variables we can also define pointer types
© J.S. Bradbury
int *p1; *p1 = 32; cout << *p1 << endl;
typedef int* IntPtr; IntPtr p1; *p1 = 32; cout << *p1 << endl;
=
CSCI 1060U Lecture 9 Slide 4
Dynamic Variables
Last Lecture § Pointers are not only used to point to ordinary (also
called automatic) variables § We can also use a pointer to create a new dynamic
variable § This variable is created and destroyed while the
program is executing
© J.S. Bradbury
int *p1;
p1 = new int; //created
delete p1; // destroyed
CSCI 1060U Lecture 9 Slide 5
Dynamic Arrays
§ We have learned how to create dynamic arrays using pointers
§ Last class we finished with an in class activity to make sure everybody understands this…
© J.S. Bradbury
IntPtr p1;
int size = 30;
p1 = new int[size];
CSCI 1060U Lecture 9 Slide 6
© J.S. Bradbury
In Class Activity #1 (Last Lecture): In C++ write a program that asks the user for the number of integers to be input. The program will create a dynamic array to store the integers in and will output the number of each integer in the array. Sample Input: Enter the size: 6 Enter the integers: 1 5 3 1 5 4 Sample Output: 1 -> 2 times 3 -> 1 time 4 -> 1 time 5 -> 2 times
CSCI 1060U Lecture 9 Slide 7
Pointers in C++ II
Overview § Today we will continue to look at the concept of
pointers § This lecture will again focus only on C++ (since Java
does not have pointers) § Some topics covered today include:
§ More on dynamic arrays § Multidimensional dynamic arrays
© J.S. Bradbury CSCI 1060U Lecture 9 Slide 8
Dynamic Variables
§ Last lecture we learned that you can destroy a dynamic variable using delete
§ However when we run the program and delete a dynamic variable we can still display its value § Why? Delete means that the memory is now free to
be used by it is not necessarily deleted
© J.S. Bradbury
int *p1; p1 = new int; //created delete p1; // destroyed
See pointers_ex4.cpp for an example.
CSCI 1060U Lecture 9 Slide 9
© J.S. Bradbury
In Class Group Activity: In C++ write a program that inputs any number of positive integers from the user. The user should input a “-1” when all of the positive integers have been entered. Use a dynamic array to store the integers.
CSCI 1060U Lecture 9 Slide 10
See pointers_ex5.cpp for the solution.
Dynamic Arrays
§ In C++ an array cannot be returned by a function § One possible solution to this problem is to return a
pointer to a dynamic array.
© J.S. Bradbury
See pointers_ex6.cpp for an example.
CSCI 1060U Lecture 9 Slide 11
Multidimensional Dynamic Arrays
§ We can use a pointer to an array of pointers to create multidimensional arrays.
§ For example the below code creates a 5 by 3 array.
© J.S. Bradbury
typedef int* ArrayPtr;
ArrayPtr *p1 = new ArrayPtr[5];
for (int i=0; i < 5; i++) {
p1[i] = new int[3];
}
CSCI 1060U Lecture 9 Slide 12
Multidimensional Dynamic Arrays
§ When using a pointer to an array of pointers to it is important to remember to delete all of the pointers
§ For example to delete our 5 by 3 array we need to
© J.S. Bradbury
for (int i=0; i < 5; i++) {
delete[ ] p1[i];
}
delete[ ] p1;
CSCI 1060U Lecture 9 Slide 13
Pointers in C++ II
Summary § We have finished discussing the concept of pointers § We have learned how to use pointers to create
multidimensional dynamic arrays Next time § Introduction to object-oriented programming
§ Structures and classes in C++
© J.S. Bradbury CSCI 1060U Lecture 9 Slide 14