Pointers
What are Pointers?
Pointers are variables that store memory addresses of other variables. They provide direct access to memory and enable dynamic memory allocation.
Basic Pointer Example
#include <iostream>
using namespace std;
int main() {
int number = 42;
int* ptr = &number; // ptr stores the address of number
cout << "Value of number: " << number << endl;
cout << "Address of number: " << &number << endl;
cout << "Value stored in ptr: " << ptr << endl;
cout << "Value pointed to by ptr: " << *ptr << endl;
// Modify value through pointer
*ptr = 100;
cout << "After modifying through pointer:" << endl;
cout << "Value of number: " << number << endl;
return 0;
}Pointer Operations
Key pointer operations include declaration, assignment, dereferencing, and arithmetic:
Pointer Operations
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Points to first element
cout << "Array traversal using pointer arithmetic:" << endl;
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << *(ptr + i) << endl;
}
cout << "\nUsing pointer increment:" << endl;
ptr = arr; // Reset pointer
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << *ptr << endl;
ptr++; // Move to next element
}
// Pointer comparison
int* start = arr;
int* end = arr + 5;
cout << "\nPointer comparison:" << endl;
cout << "Distance between pointers: " << (end - start) << endl;
return 0;
}References
References are aliases for existing variables. Unlike pointers, they cannot be null and must be initialized when declared.
References vs Pointers
#include <iostream>
using namespace std;
void swapWithPointers(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void swapWithReferences(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 10, y = 20;
cout << "Original values: x = " << x << ", y = " << y << endl;
// Using pointers
swapWithPointers(&x, &y);
cout << "After pointer swap: x = " << x << ", y = " << y << endl;
// Using references
swapWithReferences(x, y);
cout << "After reference swap: x = " << x << ", y = " << y << endl;
// Reference as alias
int number = 100;
int& ref = number; // ref is an alias for number
cout << "\nReference example:" << endl;
cout << "number = " << number << ", ref = " << ref << endl;
ref = 200; // Modifying through reference
cout << "After modifying ref: number = " << number << endl;
return 0;
}Dynamic Memory Allocation
Use new and delete operators for dynamic memory management:
Dynamic Memory Example
#include <iostream>
using namespace std;
int main() {
// Allocate single integer
int* ptr = new int(42);
cout << "Dynamically allocated integer: " << *ptr << endl;
delete ptr; // Free memory
// Allocate array
int size = 5;
int* arr = new int[size];
// Initialize array
for (int i = 0; i < size; i++) {
arr[i] = (i + 1) * 10;
}
cout << "Dynamically allocated array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
delete[] arr; // Free array memory
// Always set pointers to nullptr after deletion
ptr = nullptr;
arr = nullptr;
return 0;
}Common Pointer Pitfalls
Null Pointer Dereference
Always check if pointer is not null before dereferencing
Memory Leaks
Every new must have a corresponding delete
Dangling Pointers
Don't use pointers after the memory has been freed
Double Deletion
Don't delete the same memory twice