Topic 35 of 91 6. C++ Arrays and Strings Interactive Tutorial

Passing C++ Arrays to Functions: Parameter Techniques and Examples

14 mins read Last updated: 26 Sep 2026 19,700 views

Direct Answer & Core Concept

Passing arrays to functions in C++ can be achieved via C-style pointer decay (void f(int* arr, size_t size)), Array Reference to preserve compile-time size (void f(int (&arr)[N])), standard container pass-by-reference (const std::array& or const std::vector&), or modern C++20 std::span (non-owning contiguous view over any array buffer).

Detailed Explanation & Mechanics

1. Passing Techniques Comparison:

Architecture Diagram
ARRAY PASSING METHODS:
┌─────────────────────────┬───────────────────────────────────────────┐
│ Method                  │ Characteristics                           │
├─────────────────────────┼───────────────────────────────────────────┤
│ 1. Pointer + Size       │ void f(const int* arr, size_t len)        │
│ 2. Array Reference (&)  │ template void f(int (&arr)[N])  │
│ 3. const std::vector&│ Safe, heap-allocated, knows size          │
│ 4. std::span (C++20) │ Zero-overhead view over any array/vector  │
└─────────────────────────┴───────────────────────────────────────────┘

Runnable Code Example: Passing C++ Arrays to Functions

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <span>
#include <vector>

// Modern C++20 std::span accepts raw arrays, std::array, or std::vector
void printElements(std::span<const int> buffer) {
    std::cout << "Buffer (size: " << buffer.size() << "): ";
    for (int val : buffer) {
        std::cout << val << " ";
    }
    std::cout << "
";
}

int main() {
    int rawArray[] = {1, 2, 3, 4};
    std::vector<int> dynamicVec = {10, 20, 30, 40, 50};

    printElements(rawArray);   // Automatically creates span from raw array
    printElements(dynamicVec); // Automatically creates span from vector

    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is std::span in C++20 and why is it preferred for function parameters?

A: `std::span` is a lightweight, non-owning view over any contiguous sequence of objects (pointers + size). It accepts C-style arrays, `std::array`, and `std::vector` seamlessly without copying or memory allocations.
Q

Q: Why does sizeof(arr) return pointer size inside a function taking int arr[]?

A: In function parameter declarations, `int arr[]` is syntactic sugar for `int* arr`. The array decays to a pointer, so `sizeof(arr)` evaluates to `sizeof(int*)` (typically 8 bytes on 64-bit systems).

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Use `std::span` in C++20 for read-only array parameters. 2. In C++17 and earlier, pass `const std::vector&` or `(const T* ptr, size_t size)`. 3. Never rely on `sizeof()` on array parameters inside functions due to pointer decay.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.