C++ Pointers to Arrays

Introduction

Pointers to arrays in C++ allow you to efficiently manipulate arrays and pass them to functions. Understanding how to use pointers with arrays can help optimize memory usage and improve the flexibility of your programs.

Pointer to a Single Array

Basic Example

You can create a pointer to the first element of an array and use pointer arithmetic to access other elements.

Example: Pointer to a Single Array

#include <iostream>
using namespace std;

int main() {
    // Define an array of integers
    int numbers[] = {10, 20, 30, 40, 50};

    // Define a pointer to the first element of the array
    int* ptr = numbers;

    // Access and print elements of the array using the pointer
    for (int i = 0; i < 5; i++) {
        cout << "Element " << i << ": " << *(ptr + i) << endl;
    }

    return 0;
}

Output

Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Explanation

  • int* ptr = numbers; sets ptr to point to the first element of the array numbers.
  • *(ptr + i) accesses the elements of the array using pointer arithmetic.

Pointer to an Array of Arrays (2D Arrays)

You can create a pointer to a 2D array (array of arrays) and use it to manipulate the 2D array elements.

Example: Pointer to a 2D Array

#include <iostream>
using namespace std;

int main() {
    // Define a 2D array (3x3 matrix)
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Define a pointer to the first element of the 2D array
    int (*ptr)[3] = matrix;

    // Access and print elements of the 2D array using the pointer
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Element [" << i << "][" << j << "]: " << *(*(ptr + i) + j) << endl;
        }
    }

    return 0;
}

Output

Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9

Explanation

  • int (*ptr)[3] = matrix; sets ptr to point to the first row of the 2D array matrix.
  • *(*(ptr + i) + j) accesses the elements of the 2D array using pointer arithmetic.

Pointer to an Array of Pointers

You can also create an array of pointers, where each pointer points to an array.

Example: Pointer to an Array of Pointers

#include <iostream>
using namespace std;

int main() {
    // Define three arrays
    int arr1[] = {1, 2, 3};
    int arr2[] = {4, 5, 6};
    int arr3[] = {7, 8, 9};

    // Define an array of pointers
    int* ptrArray[3] = {arr1, arr2, arr3};

    // Access and print elements using the array of pointers
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Element [" << i << "][" << j << "]: " << *(ptrArray[i] + j) << endl;
        }
    }

    return 0;
}

Output

Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9

Explanation

  • int* ptrArray[3] = {arr1, arr2, arr3}; defines an array of pointers, where each pointer points to an array.
  • *(ptrArray[i] + j) accesses the elements of the arrays using the array of pointers.

Passing Arrays to Functions Using Pointers

You can pass arrays to functions using pointers, which allows the function to modify the original array elements.

Example: Passing Arrays to Functions

#include <iostream>
using namespace std;

// Function to double the elements of an array
void doubleArray(int* arr, int size) {
    for (int i = 0; i < size; i++) {
        arr[i] *= 2;
    }
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Pass the array to the function
    doubleArray(numbers, size);

    // Print the modified array
    for (int i = 0; i < size; i++) {
        cout << "Element " << i << ": " << numbers[i] << endl;
    }

    return 0;
}

Output

Element 0: 2
Element 1: 4
Element 2: 6
Element 3: 8
Element 4: 10

Explanation

  • The doubleArray function takes a pointer to the first element of the array and the size of the array as arguments.
  • The function modifies the original array elements by doubling their values.

Example Programs

Example 1: Finding the Largest Element in an Array

#include <iostream>
using namespace std;

// Function to find the largest element in an array
int findLargest(int* arr, int size) {
    int largest = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > largest) {
            largest = arr[i];
        }
    }
    return largest;
}

int main() {
    int numbers[] = {10, 50, 20, 40, 30};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Find the largest element
    int largest = findLargest(numbers, size);

    cout << "The largest element is: " << largest << endl;

    return 0;
}

Output

The largest element is: 50

Explanation

  • The findLargest function takes a pointer to the first element of the array and the size of the array as arguments.
  • The function iterates through the array to find and return the largest element.

Example 2: Transposing a 2D Array

#include <iostream>
using namespace std;

// Function to transpose a 2D array
void transpose(int (*matrix)[3], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = i + 1; j < cols; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }
}

int main() {
    // Define a 2D array (3x3 matrix)
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Transpose the matrix
    transpose(matrix, 3, 3);

    // Print the transposed matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Output

1 4 7
2 5 8
3 6 9

Explanation

  • The transpose function takes a pointer to the first element of the 2D array and the dimensions of the array as arguments.
  • The function swaps the elements to transpose the matrix.

Conclusion

Pointers to arrays in C++ allow for efficient array manipulation and provide flexibility in passing arrays to functions. This chapter covered the basics of defining and using pointers to single arrays, 2D arrays, and arrays of pointers. It also demonstrated how to pass arrays to functions using pointers. Example programs illustrated practical applications of pointers to arrays, including finding the largest element in an array and transposing a 2D array. Understanding how to use pointers to arrays effectively is essential for writing efficient and flexible C++ programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top