C++ Pointers & Arrays
In this page:
data_type array_name[] = {value1, value2, value3};
data_type *pointer_name = array_name;
*(pointer_name + index);
Array Name as Pointer
In C++, the name of an array acts as a constant pointer to its first element, so you can assign the array name directly to a pointer variable without using the address-of operator.
This is why functions that accept arrays are actually receiving a pointer, not a copy of the whole block.
Example: Array Name as Pointer
#include <iostream>
int main() {
int arr[] = {1, 2, 3};
int *ptr = arr; // array name decays to a pointer to its first element
std::cout << *ptr << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Arithmetic
You can use pointer arithmetic to navigate through an array: incrementing a pointer with ptr++ moves it forward by exactly sizeof(type) bytes, not one byte, so the compiler automatically scales the jump to the element type.
This lets you walk a buffer without ever writing an index variable.
Example: Pointer Arithmetic
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {1, 2, 3};
int *ptr = arr;
ptr++;
// Print to the console with cout
std::cout << *ptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Array Elements with Pointers
Instead of using brackets like arr[i], you can use dereferenced pointer offsets like *(ptr + i) to read or modify any element in an array, and in fact arr[i] is defined by the language as shorthand for exactly that expression.
Knowing this equivalence explains a lot of pointer-heavy legacy C++ code.
Example: Accessing Array Elements with Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
// Print to the console with cout
std::cout << *(ptr + 1) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamically Allocated Arrays
You can allocate dynamic arrays on the heap using the 'new[]' operator, which lets you set the array size at runtime instead of at compile time.
Remember to release this memory using 'delete[]' when finished, since forgetting the brackets or the call entirely leaks memory silently.
Example: Dynamically Allocated Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare size and set it to 3
int size = 3;
// Allocate an object on the heap with new
int *arr = new int[size];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
// Print to the console with cout
std::cout << arr[1] << std::endl;
// Free the heap memory with delete
delete[] arr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Arrays to Functions using Pointers
When you pass an array to a function, it decays into a pointer to its first element rather than being copied, which is efficient but also means the function loses the array's size information.
That's why array-processing functions usually take an explicit length parameter alongside the pointer.
Example: Passing Arrays to Functions using Pointers
// Include std::cout and std::cin
#include <iostream>
void printFirst(int *arr) {
// Print to the console with cout
std::cout << arr[0] << std::endl;
}
// Program execution starts in main()
int main() {
int nums[] = {5, 10, 15};
printFirst(nums);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming array name can be reassigned like a pointer, like
arr = p;. - Using
sizeofon a pointer to find the array length. - Using
deleteinstead ofdelete[]for dynamic arrays.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: