C++

In C++, the relationship between pointers, arrays, and functions is so intimate that they are often theoretically inseparable. To master C++, one must understand how arrays "decay" into pointers and how functions use this to manipulate large sets of data efficiently.

1. The Theory: Array Decay and Pointer Equivalence

Theoretically, the name of an array is a constant pointer to its first element.

A. Array Decay

When you use an array name in an expression (like passing it to a function), it "decays" into a pointer to its first element.

  • If you have int arr[5], the identifier arr is equivalent to &arr[0].

  • Because of this, the compiler treats arr[i] and *(arr + i) as exactly the same operation.

B. The Identity of Types

In a function parameter list, the following three declarations are identical to the compiler:

  1. void func(int* p);

  2. void func(int p[]);

  3. void func(int p[10]); // The '10' is ignored by the compiler!

2. Passing Arrays to Functions (Call by Reference)

When you pass an array to a function, you are technically performing a Call by Reference (via a pointer).

  • Efficiency: You are not copying the entire array (which could be millions of items). You are only copying the address of the first item (typically 8 bytes).

  • Side Effects: Because the function receives the actual memory address, any changes made to the array elements inside the function will change the original array in the caller's scope.

3. The "Size" Problem (Long Theory)

One of the most dangerous aspects of pointers with arrays is the loss of size information.

The Theory of Pointer Erasure

When an array is passed to a function, it loses its "array-ness" and becomes a simple pointer. Inside the function, if you try to use sizeof(arr), it will return the size of the pointer (usually 8 bytes), NOT the size of the whole array.

The Solution: You must always pass a second argument to the function: the Size. Without the size, the function has no theoretical way of knowing where the array ends, leading to potential buffer overflows.

4. Pointers to Functions (The "Callback" Theory)

Pointers don't just point to data; they can point to code. A Function Pointer stores the starting address of the executable code of a function.

  • Usage: This allows you to pass a function as an argument to another function. This is the basis of "Plug-and-Play" code or "Callbacks."

  • Example: You could write a sort function and pass it a pointer to a comparison function. This allows the same sort code to sort numbers in ascending or descending order depending on which function pointer you provide.

5. Practical Code Example

This program demonstrates passing an array to a function via a pointer and modifying the original values.

C++
#include <iostream>
using namespace std;

// Function takes a pointer to the array and its size
// Note: 'int* arr' is the same as 'int arr[]'
void doubleElements(int* arr, int size) {
    for (int i = 0; i < size; i++) {
        // Using pointer arithmetic to modify original data
        *(arr + i) *= 2; 
    }
}

void printArray(const int* arr, int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " "; // Standard indexing works on pointers too
    }
    cout << endl;
}

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

    cout << "Original array: ";
    printArray(myNumbers, n);

    // Pass the array name (it decays to a pointer)
    doubleElements(myNumbers, n);

    cout << "After doubling: ";
    printArray(myNumbers, n);

    return 0;
}

6. Interaction Rules

ScenarioTheoretical Behavior
Passing arrPasses the address of the first element (&arr[0]).
Function ModificationChanges are permanent (Call by Reference).
sizeof inside functionReturns pointer size, not array size.
*(arr + i)Accesses the $i$-th element via pointer arithmetic.
const int* arrPasses the array by reference but prevents modification.
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now