Why can't we pass arrays to function by value? Why can't we pass arrays to function by value? arrays arrays

Why can't we pass arrays to function by value?


The origin is historical. The problem is that the rule "arrays decay into pointers, when passed to a function" is simple.

Copying arrays would be kind of complicated and not very clear, since the behavior would change for different parameters and different function declarations.

Note that you can still do an indirect pass by value:

struct A { int arr[2]; };void func(struct A);


Here's another perspective: There isn't a single type "array" in C. Rather, T[N] is a a different type for every N. So T[1], T[2], etc., are all different types.

In C there's no function overloading, and so the only sensible thing you could have allowed would be a function that takes (or returns) a single type of array:

void foo(int a[3]);  // hypothetical

Presumably, that was just considered far less useful than the actual decision to make all arrays decay into a pointer to the first element and require the user to communicate the size by other means. After all, the above could be rewritten as:

void foo(int * a){  static const unsigned int N = 3;  /* ... */}

So there's no loss of expressive power, but a huge gain in generality.

Note that this isn't any different in C++, but template-driven code generation allows you to write a templated function foo(T (&a)[N]), where N is deduced for you -- but this just means that you can create a whole family of distinct, different functions, one for each value of N.

As an extreme case, imagine that you would need two functions print6(const char[6]) and print12(const char[12]) to say print6("Hello") and print12("Hello World") if you didn't want to decay arrays to pointers, or otherwise you'd have to add an explicit conversion, print_p((const char*)"Hello World").


Answering a very old question, as Question is market with C++ just adding for completion purposes, we can use std::array and pass arrays to functions by value or by reference which gives protection against accessing out of bound indexes:

below is sample:

#include <iostream>#include <array>//pass array by referencetemplate<size_t N>void fill_array(std::array<int, N>& arr){    for(int idx = 0; idx < arr.size(); ++idx)        arr[idx] = idx*idx;}//pass array by valuetemplate<size_t N>void print_array(std::array<int, N> arr){    for(int idx = 0; idx < arr.size(); ++idx)        std::cout << arr[idx] << std::endl;}int main(){    std::array<int, 5> arr;    fill_array(arr);    print_array(arr);    //use different size    std::array<int, 10> arr2;    fill_array(arr2);    print_array(arr2);}