C++ Chapter 10 (L 24)
C++ Chapter 10 (L 24)
(Mandalay)
Chapter 10
Pointers
Pointer
char* ptr1, * ptr2, *ptr3; Declaration
int *ptr4, *ptr5;
A pointer variable can hold the address of any variable of the correct type; it’s a
receptacle awaiting an address.
Object-Oriented Programming in C++, 4th Edition 8
Accessing the Variable Pointed To
(Example)
#include <iostream>
using namespace std;
int main()
{
int var1, var2; // declarations of two variables
int* ptr: // declaration of pointer variable
var1 = 11; // value 11 is assigned to var1
var2 = 22; // value 22 is assigned to var2
ptr = &var1; //pointer points to var1
cout <<“var1:”<< *ptr << endl; //print contents of pointer (11)
ptr = &var2; //pointer points to var2
var1:11
cout <<“var2:”<< *ptr << endl; //print contents of pointer (22)
var2:22
return 0;
}
Object-Oriented Programming in C++, 4th Edition 9
Next Lecture