Pointers let you directly access memory locations. Arrays are groups of items stored next to each other in memory. Using pointers with arrays helps you work with data efficiently.
Pointers and arrays in C++
class PointerArrayExample { public: void printArrayUsingPointer(int* array, int size) { for (int i = 0; i < size; i++) { // Access element using pointer arithmetic std::cout << *(array + i) << " "; } std::cout << std::endl; } void printArrayUsingIndex(int array[], int size) { for (int i = 0; i < size; i++) { // Access element using array index std::cout << array[i] << " "; } std::cout << std::endl; } };
In C++, the name of an array acts like a pointer to its first element.
Pointer arithmetic means adding an integer to a pointer moves it to the next elements in memory.
#include <iostream> int main() { int numbers[3] = {10, 20, 30}; int* pointerToNumbers = numbers; // points to first element std::cout << *pointerToNumbers << std::endl; // prints 10 std::cout << *(pointerToNumbers + 1) << std::endl; // prints 20 std::cout << *(pointerToNumbers + 2) << std::endl; // prints 30 return 0; }
#include <iostream> int main() { // int emptyArray[0]; // zero-length arrays are not standard in C++ int* pointerToEmpty = nullptr; // No elements to access, pointer points to no valid data std::cout << pointerToEmpty << std::endl; // prints address or nullptr return 0; }
#include <iostream> int main() { int singleElementArray[1] = {42}; int* pointerToSingle = singleElementArray; std::cout << *pointerToSingle << std::endl; // prints 42 return 0; }
#include <iostream> int main() { int numbers[5] = {5, 10, 15, 20, 25}; int* pointerToNumbers = numbers; // Access first element std::cout << *pointerToNumbers << std::endl; // 5 // Access last element std::cout << *(pointerToNumbers + 4) << std::endl; // 25 return 0; }
This program shows two ways to print array elements: using pointers and using array indexes. Both print the same result.
#include <iostream> class PointerArrayExample { public: void printArrayUsingPointer(int* array, int size) { std::cout << "Using pointer arithmetic: "; for (int i = 0; i < size; i++) { std::cout << *(array + i) << " "; } std::cout << std::endl; } void printArrayUsingIndex(int array[], int size) { std::cout << "Using array index: "; for (int i = 0; i < size; i++) { std::cout << array[i] << " "; } std::cout << std::endl; } }; int main() { int numbers[5] = {100, 200, 300, 400, 500}; PointerArrayExample example; std::cout << "Array elements:" << std::endl; example.printArrayUsingPointer(numbers, 5); example.printArrayUsingIndex(numbers, 5); return 0; }
Time complexity to access any element by pointer or index is O(1).
Pointer arithmetic moves the pointer by the size of the data type automatically.
Common mistake: dereferencing pointers outside array bounds causes undefined behavior.
Use pointers when you want efficient access or to pass arrays to functions without copying.
Pointers can access array elements by moving through memory addresses.
Array name acts like a pointer to the first element.
Pointer arithmetic and array indexing both access elements but pointer arithmetic works with memory addresses directly.
Practice
Solution
Step 1: Understand array name behavior
In C++, the name of an array acts as a pointer to its first element, not the whole array or size.Step 2: Confirm pointer usage
This pointer can be used to access elements by pointer arithmetic or indexing.Final Answer:
A pointer to the first element of the array -> Option DQuick Check:
Array name = pointer to first element [OK]
- Thinking array name stores size
- Confusing array name with last element
- Assuming array name copies data
arr?Solution
Step 1: Understand pointer declaration
To declare a pointer to an integer, useint *ptr. The array namearracts as a pointer to its first element.Step 2: Assign pointer correctly
Assigningptr = arr;sets the pointer to the first element's address. Using&arrgives the address of the whole array, which has a different type.Final Answer:
int *ptr = arr; -> Option CQuick Check:
Pointer to first element = int *ptr = arr [OK]
- Using &arr instead of arr for pointer
- Declaring pointer as int instead of int*
- Assigning pointer without *
int arr[] = {10, 20, 30};
int *p = arr;
std::cout << *(p + 1) << std::endl;Solution
Step 1: Understand pointer arithmetic
Pointerppoints toarr[0]. Adding 1 moves pointer toarr[1].Step 2: Dereference pointer at new position
Dereferencing*(p + 1)gives the value atarr[1], which is 20.Final Answer:
20 -> Option AQuick Check:
*(p + 1) = arr[1] = 20 [OK]
- Confusing pointer arithmetic with addition of values
- Dereferencing pointer without offset
- Expecting compilation error
int arr[3] = {1, 2, 3};
int *p = &arr;
std::cout << *(p + 2) << std::endl;Solution
Step 1: Analyze pointer assignment
arris an array name acting as pointer to first element.&arris address of whole array, typeint (*)[3], incompatible withint *.Step 2: Correct pointer assignment
Assigningint *p = arr;is correct to point to first element. Using&arrcauses type mismatch and incorrect pointer arithmetic.Final Answer:
Incorrect pointer assignment: &arr should be arr -> Option BQuick Check:
Use arr, not &arr, for int* pointer [OK]
- Using &arr instead of arr for int*
- Ignoring pointer type mismatch warnings
- Assuming &arr points to first element
int arr[] = {5, 10, 15, 20};
int *p = arr + 1;
std::cout << p[2] << std::endl;Solution
Step 1: Understand pointer initialization
p = arr + 1;points toarr[1], which is 10.Step 2: Access element using pointer indexing
p[2]means *(p + 2), which points toarr[3](1 + 2 = 3), value 20.Final Answer:
20 -> Option AQuick Check:
p[2] = arr[3] = 20 [OK]
- Confusing p[2] with arr[2]
- Forgetting pointer offset starts at arr + 1
- Assuming p[2] is out of bounds
Ready to prove it?
Pass the test and this pattern moves unknown
