C++ Pointers and Arrays


Introduction

In C++, pointers and arrays are fundamental concepts that are closely linked. A pointer is a variable that stores the memory address of another variable, and arrays are collections of elements stored in contiguous memory locations. Pointers and arrays together offer powerful capabilities for efficiently accessing and manipulating array elements.

In this guide, we’ll explore:

  • How arrays and pointers are related in C++.
  • How to use pointers to access and manipulate arrays.
  • Pointer arithmetic for traversing arrays.
  • How to pass arrays to functions using pointers.

Let’s dive into the details!


1. Pointers and Arrays: The Connection

In C++, arrays and pointers are closely related. An array name is actually a pointer to the first element of the array. This means that when you pass an array to a function, you're essentially passing a pointer to the first element of that array.

Array Declaration:

int arr[] = {1, 2, 3, 4, 5};

In memory, this array is stored as a contiguous block of memory, where arr[0] is the first element, arr[1] is the second element, and so on.

Pointer to Array:

You can create a pointer to the array like this:

int* ptr = arr;  // 'arr' points to the first element

In this case, ptr is a pointer to the first element of the array arr.


2. Accessing Array Elements with Pointers

Since the array name is equivalent to a pointer to the first element, you can use pointer arithmetic to access the array elements.

Example:

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int* ptr = arr;  // Pointer to the first element

    // Accessing array elements using the pointer
    cout << "First element: " << *ptr << endl;       // 10
    cout << "Second element: " << *(ptr + 1) << endl; // 20
    cout << "Third element: " << *(ptr + 2) << endl;  // 30

    return 0;
}

Output:

First element: 10
Second element: 20
Third element: 30

Explanation:

  • *ptr dereferences the pointer to access the value of the first element of the array (arr[0]).
  • *(ptr + 1) uses pointer arithmetic to move to the next memory location (the second element), and so on.

3. Pointer Arithmetic with Arrays

In C++, you can perform arithmetic operations on pointers. When you increment or decrement a pointer, it moves by the size of the data type it points to. For example, incrementing a pointer to an int moves it by sizeof(int) bytes.

Example:

#include <iostream>
using namespace std;

int main() {
    int arr[] = {100, 200, 300, 400, 500};
    int* ptr = arr;  // Pointer to the first element

    // Pointer arithmetic to access array elements
    cout << "First element: " << *ptr << endl;    // 100
    ptr++;  // Move the pointer to the next element
    cout << "Second element: " << *ptr << endl;   // 200
    ptr += 2;  // Move the pointer ahead by 2 elements
    cout << "Fourth element: " << *ptr << endl;   // 400

    return 0;
}

Output:

First element: 100
Second element: 200
Fourth element: 400

Explanation:

  • ptr++ increments the pointer, moving it to the next element in the array (arr[1]).
  • ptr += 2 increments the pointer by two elements, so it points to arr[3].

4. Passing Arrays to Functions using Pointers

When you pass an array to a function in C++, what you're actually passing is a pointer to the first element of the array. This allows the function to modify the original array elements.

Example: Passing Arrays to Functions

#include <iostream>
using namespace std;

// Function that modifies the array using a pointer
void modifyArray(int* ptr) {
    for (int i = 0; i < 5; i++) {
        *(ptr + i) *= 2;  // Multiply each element by 2
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    
    // Pass array to function (passed as pointer)
    modifyArray(arr);

    // Output modified array
    cout << "Modified array: ";
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << " ";  // Output: 2 4 6 8 10
    }

    return 0;
}

Output:

Modified array: 2 4 6 8 10

Explanation:

  • In the modifyArray function, the parameter int* ptr is a pointer to the first element of the array.
  • Inside the function, pointer arithmetic (*(ptr + i)) is used to access and modify each element of the array.

5. Pointers to Multidimensional Arrays

In C++, multidimensional arrays are essentially arrays of arrays. You can use pointers to handle multidimensional arrays as well.

Example: Pointer to a 2D Array

#include <iostream>
using namespace std;

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int* ptr = &arr[0][0];  // Pointer to the first element of the 2D array

    // Access elements using pointer arithmetic
    cout << "First element: " << *ptr << endl;       // 1
    cout << "Second element: " << *(ptr + 1) << endl; // 2
    cout << "Fourth element: " << *(ptr + 3) << endl; // 4

    return 0;
}

Output:

First element: 1
Second element: 2
Fourth element: 4

Explanation:

  • ptr points to the first element of the 2D array arr[0][0].
  • Pointer arithmetic is used to access the elements in the 2D array, treating it as a contiguous block of memory.

6. Array of Pointers

Sometimes, you may need an array of pointers where each element in the array is a pointer to another variable or array.

Example: Array of Pointers to Strings

#include <iostream>
using namespace std;

int main() {
    const char* arr[] = {"Hello", "World", "C++"};

    // Accessing elements using pointers
    for (int i = 0; i < 3; i++) {
        cout << *(arr + i) << " ";  // Dereferencing the pointer in the array
    }

    return 0;
}

Output:

Hello World C++

Explanation:

  • arr[] is an array of pointers, where each element is a pointer to a string (a const char*).
  • *(arr + i) dereferences the pointer stored in arr[i] to access each string.