Introduction
Pointers in C++ are variables that store memory addresses of other variables. They are a powerful feature that allows for direct memory access and manipulation. Understanding pointers is crucial for dynamic memory management, efficient array handling, and for implementing complex data structures like linked lists and trees.
Declaring and Initializing Pointers
Syntax for Declaring a Pointer
dataType *pointerName;
Example: Declaring and Initializing a Pointer
#include <iostream>
using namespace std;
int main() {
int x = 10; // Declare an integer variable
int *ptr = &x; // Declare a pointer and initialize it with the address of x
// Access and print the value and address
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl;
cout << "Pointer ptr points to: " << ptr << endl;
cout << "Value at address pointed by ptr: " << *ptr << endl;
return 0;
}
Output
Value of x: 10
Address of x: 0x61fe14
Pointer ptr points to: 0x61fe14
Value at address pointed by ptr: 10
Explanation
int *ptr = &x;
declares a pointerptr
that stores the address of the variablex
.*ptr
accesses the value stored at the address pointed to byptr
.
Pointer Arithmetic
Pointer arithmetic allows for operations such as incrementing or decrementing pointers to traverse arrays or memory blocks.
Example: Pointer Arithmetic
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array
for (int i = 0; i < 5; i++) {
cout << "Value at arr[" << i << "] = " << *ptr << endl;
ptr++; // Move the pointer to the next element
}
return 0;
}
Output
Value at arr[0] = 10
Value at arr[1] = 20
Value at arr[2] = 30
Value at arr[3] = 40
Value at arr[4] = 50
Explanation
ptr++
increments the pointer to point to the next element in the array.- The loop prints the values of the array elements using the pointer.
Pointers and Arrays
Pointers can be used to access and manipulate array elements efficiently.
Example: Using Pointers with Arrays
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array
for (int i = 0; i < 5; i++) {
cout << "arr[" << i << "] = " << *(ptr + i) << endl; // Access array elements using pointer
}
return 0;
}
Output
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
Explanation
*(ptr + i)
accesses the array elements using the pointer and pointer arithmetic.
Pointers to Pointers
A pointer to a pointer is a variable that stores the address of another pointer.
Example: Pointer to a Pointer
#include <iostream>
using namespace std;
int main() {
int x = 10;
int *ptr = &x;
int **pptr = &ptr; // Pointer to pointer
// Access and print the value and addresses
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl;
cout << "Pointer ptr points to: " << ptr << endl;
cout << "Value at address pointed by ptr: " << *ptr << endl;
cout << "Pointer pptr points to: " << pptr << endl;
cout << "Value at address pointed by pptr: " << *pptr << endl;
cout << "Value at address pointed by *pptr: " << **pptr << endl;
return 0;
}
Output
Value of x: 10
Address of x: 0x61fe14
Pointer ptr points to: 0x61fe14
Value at address pointed by ptr: 10
Pointer pptr points to: 0x61fe10
Value at address pointed by pptr: 0x61fe14
Value at address pointed by *pptr: 10
Explanation
int **pptr = &ptr;
declares a pointer to a pointerpptr
that stores the address of the pointerptr
.
Dynamic Memory Allocation
Dynamic memory allocation allows for allocating memory at runtime using pointers.
Example: Dynamic Memory Allocation with new
and delete
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // Allocate memory dynamically
*ptr = 10; // Assign value to the allocated memory
cout << "Value at allocated memory: " << *ptr << endl;
delete ptr; // Deallocate memory
return 0;
}
Output
Value at allocated memory: 10
Explanation
int *ptr = new int;
allocates memory dynamically.delete ptr;
deallocates the memory.
Example: Dynamic Array Allocation
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int *arr = new int[size]; // Allocate memory for an array dynamically
// Initialize and print the array
for (int i = 0; i < size; i++) {
arr[i] = i * 10;
cout << "arr[" << i << "] = " << arr[i] << endl;
}
delete[] arr; // Deallocate the memory
return 0;
}
Output
Enter the size of the array: 5
arr[0] = 0
arr[1] = 10
arr[2] = 20
arr[3] = 30
arr[4] = 40
Explanation
int *arr = new int[size];
allocates memory dynamically for an array of the specified size.delete[] arr;
deallocates the memory for the array.
Example Programs
Example 1: Swapping Values Using Pointers
This example demonstrates swapping values of two variables using pointers.
#include <iostream>
using namespace std;
// Function to swap values using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
cout << "Before swap:" << endl;
cout << "x = " << x << ", y = " << y << endl;
swap(&x, &y); // Pass the addresses of x and y
cout << "After swap:" << endl;
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
Output
Before swap:
x = 5, y = 10
After swap:
x = 10, y = 5
Explanation
- The
swap
function uses pointers to swap the values ofx
andy
.
Example 2: Dynamic Memory Allocation for a Matrix
This example demonstrates dynamic memory allocation for a 2D matrix.
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
// Allocate memory for the matrix
int **matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
// Initialize and print the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = i * cols + j;
cout << matrix[i][j] << " ";
}
cout << endl;
}
// Deallocate memory
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;
return 0;
}
Output
Enter the number of rows and columns: 3 3
0 1 2
3 4 5
6 7 8
Explanation
- The program dynamically allocates memory for a 2D matrix based on user input.
- The matrix is initialized and printed.
- Memory is deallocated after use.
Conclusion
Pointers in C++ are used for direct memory access and manipulation. This chapter covered how to declare and use pointers, perform pointer
arithmetic, use pointers with arrays, work with pointers to pointers, and manage dynamic memory allocation. Example programs demonstrated swapping values using pointers and dynamically allocating memory for a matrix. Understanding how to use pointers effectively will help you write more efficient and flexible code.