Returning Arrays

Can I Pass an Array to a Function in C?

Yes. In C, an array is passed as a pointer to its first element, so you must pass its length too. This guide shows a working example and the common mistakes.

On this page
  1. Short answer
  2. Passing an array in C
  3. Why the length has to be passed
  4. Parameter forms
  5. Two-dimensional arrays
  6. Passing arrays in C++
  7. Common mistakes

Short answer

Yes. In C, you can pass an array to a function. The function doesn’t receive the whole array, though. It receives a pointer to the first element, so the length must be passed as a separate argument.

Passing an array in C

If arrays are still new, what is an array in C with an example is a good place to start. The example below passes an array to two functions, one that reads it and one that changes it.

#include <stddef.h>
#include <stdio.h>

void print_values(const int values[], size_t count) {
    for (size_t i = 0; i < count; i++) {
        printf("%d\n", values[i]);
    }
}

void double_values(int values[], size_t count) {
    for (size_t i = 0; i < count; i++) {
        values[i] *= 2;
    }
}

int main(void) {
    int scores[] = {70, 85, 90};
    size_t count = sizeof scores / sizeof scores[0];

    double_values(scores, count);
    print_values(scores, count);  /* prints 140, 170, and 180, one per line */
    return 0;
}

scores is passed by name, and that name turns into a pointer to its first element. double_values changes the caller’s array because it works through that pointer, so the change is visible in main. The count is computed in main, where the full size of the array is still known.

Why the length has to be passed

Inside double_values, values is just a pointer. So sizeof values gives the size of a pointer, not the size of the array. The sizeof scores / sizeof scores[0] trick only works where the array is declared, not inside a function that receives it. Pass the count instead.

Parameter forms

These three declarations mean the same thing in a function parameter list:

  • void f(int values[])
  • void f(int *values)
  • void f(int values[10])

In the third form, the 10 is ignored by the compiler. C99 also allows int values[static 10], which tells the compiler the caller should pass at least 10 elements. Compilers can warn about some mismatches, but the rule isn’t enforced at runtime.

Two-dimensional arrays

Only the first dimension of an array parameter can be left empty. For int grid[][3], the second dimension must be a fixed number, so the compiler knows how far apart the rows are. The first dimension is passed as a separate count.

#include <stddef.h>
#include <stdio.h>

void print_grid(const int grid[][3], size_t rows) {
    for (size_t r = 0; r < rows; r++) {
        for (size_t c = 0; c < 3; c++) {
            printf("%d ", grid[r][c]);
        }
        printf("\n");
    }
}

C99 variable-length array parameters can take the size from an earlier argument, but they are less common in practice.

Passing arrays in C++

C++ can pass arrays the same way. A reference to a fixed-size array also keeps the size, so the function can read it without a separate count.

#include <cstddef>
#include <iostream>

template <std::size_t N>
void print_all(const int (&values)[N]) {
    for (std::size_t i = 0; i < N; ++i) {
        std::cout << values[i] << '\n';
    }
}

int main() {
    int scores[] = {70, 85, 90};
    print_all(scores);  // N is deduced as 3
}

This works only with real arrays, not with pointers. In C++20, std::span<const int> can also take an array or a vector.

Common mistakes

  • Using sizeof on a parameter. Inside the function, sizeof measures the pointer, so the length has to come from the caller.
  • Passing the wrong length. If the count is larger than the real number of elements, the function reads past the end of the array, which is undefined behavior.
  • Trying to return an array. A function can’t return a C array this way. Returning an array is a different problem, and how to return an array in c covers the options.

The complete guide to returning arrays

How to Return an Array in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Tuples in C++ With std::tuple

Learn how std::tuple groups a fixed number of values of mixed types in C++. Covers creating tuples, reading them with std::get and structured bindings.

Tuples 4 min read

NULL vs nullptr in C++

NULL is an older macro, usually defined as 0. nullptr, added in C++11, is a typed null pointer that avoids overload surprises. Here's how to choose.

Is C++ Hard to Learn?

Is C++ hard to learn? This guide explains which parts trip up beginners, how C++ compares with C, and a practical order for learning it step by step.

Is C++ Hard 5 min read