0
0
C++programming~5 mins

Pointers and arrays in C++

Choose your learning style9 modes available
Introduction

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.

When you want to loop through an array quickly using memory addresses.
When you need to pass large arrays to functions without copying all data.
When you want to access or modify array elements using pointer arithmetic.
When working with dynamic arrays created with manual memory allocation.
When you want to understand how data is stored and accessed in memory.
Syntax
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.

Examples
Pointer points to the first element of the array. Using pointer + index accesses other elements.
C++
#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;
}
Empty array has no elements. Pointer points to no valid data. Be careful not to dereference.
C++
#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;
}
Array with one element. Pointer points to that single element.
C++
#include <iostream>

int main() {
    int singleElementArray[1] = {42};
    int* pointerToSingle = singleElementArray;

    std::cout << *pointerToSingle << std::endl; // prints 42

    return 0;
}
Shows accessing first and last elements using pointer arithmetic.
C++
#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;
}
Sample Program

This program shows two ways to print array elements: using pointers and using array indexes. Both print the same result.

C++
#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;
}
OutputSuccess
Important Notes

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.

Summary

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.