Page 78 - PowerPoint Presentation
P. 78
CAVITE STATE UNIVERSITY
T3 CAMPUS
Department of Information Technology DCIT 25 – Data Structures and Algorithms
The pointer to a pointer variable is assigned the memory address of the ptInitial pointer.
Programmers use a pointer to a pointer to tell a computer to use the contents of the
memory address contained in the pointer variable that the pointer to a pointer is pointing to.
This is mouthful, so we’ll restate this using an example:
You can use the content of the initial1 variable by referencing the ptPtInitial variable.
Here’s how this is done: cout << **ptPtInitial;
The cout statement is used in C++ to display something on the screen. In this example,
you’re displaying the content of the initial1 variable, although it doesn’t seem to be doing so.
This statement is telling the computer to go to the memory address stored in the ptPtInitial
pointer to a pointer variable, which is memory address 5. The content of that memory address
is another memory address, which is memory address 1. The computer is told to go to the
memory address 1 and display the content of that memory address, which is the letter D.
Two memory addresses are referenced when using a pointer to a pointer to display a value
on the screen.
QUIZ 4. Question and Answer (3 pts each)
1. What is a pointer?
2. Why would you use a pointer in a program?
3. What is a pointer to a pointer?
4. Why would you use a pointer to a pointer in a program?
5. What is shown on the screen if you display the content of a pointer variable?
Page | 25