Chapter 7-Pointers
Chapter 7-Pointers
In C++, a pointer refers to a variable that holds the address of another variable. Like regular
variables, pointers have a data type. For example, a pointer of type integer can hold the address
of a variable of type integer. A pointer of character type can hold the address of a variable of
character type.
You should see a pointer as a symbolic representation of a memory address. With pointers,
programs can simulate call-by-reference. They can also create and manipulate dynamic data
structures. In C++, a pointer variable refers to a variable pointing to a specific address in a
memory pointed by another variable.
Addresses in C++
To understand C++ pointers, you must understand how computers store data. When you create a
variable in your C++ program, it is assigned some space the computer memory. The value of this
variable is stored in the assigned location.
To know the location in the computer memory where the data is stored, C++ provides the &
(reference) operator. The operator returns the address that a variable occupies. For example, if x
is a variable, &x returns the address of the variable.
Page | 1
Programming I 2023
Chapter Seven Pointers
7.3. Reference operator (&) and Deference operator (*)
The reference operator (&) returns the variable's address.
The dereference operator (*) helps us get the value that has been stored in a memory
address.
For example: If we have a variable given the name num, stored in the address 0x234 and storing
the value 28.
Example 1:
#include <iostream>
using namespace std;
int main() { Program Output
int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl;
return 0;
}
Page | 2
Programming I 2023
Chapter Seven Pointers
#include <iostream.h>
int main() {
int *ip;
int arr[] = { 10, 34, 13, 76, 5, 46 }; Program Output
ip = arr;
for (int x = 0; x < 6; x++) { 10
cout << *ip << endl; 34
ip++; 13
} 76
return 0; 5
} 46
Page | 3