Skip to content
C Basics: Pass Array Lengths Explicitly to Functions

C Basics: Pass Array Lengths Explicitly to Functions

November 1, 2026

A function receiving an array parameter does not automatically know the number of elements. Calculate the count at the declaration site, then pass it with the data.

Prepare a scratch directory

Tested on Linux x86_64 with GCC 14.2.0. Install GCC and the C standard-library development headers, and make gcc available on PATH. You also need Bash, a text editor, and mktemp, rm, and rmdir. No third-party C library is required.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save this as array-length-parameters-demo.c:

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

static bool maximum_value(const int *values, size_t count, int *maximum)
{
    if (count == 0 || values == NULL || maximum == NULL) {
        return false;
    }
    int candidate = values[0];
    for (size_t index = 1; index < count; ++index) {
        if (values[index] > candidate) {
            candidate = values[index];
        }
    }
    *maximum = candidate;
    return true;
}

int main(void)
{
    const int samples[] = {12, 7, 19, 4};
    const size_t count = sizeof samples / sizeof samples[0];
    int maximum = 0;
    if (!maximum_value(samples, count, &maximum)) {
        return 1;
    }
    printf("Count: %zu\n", count);
    printf("Maximum: %d\n", maximum);
    printf("Empty input rejected: %s\n",
           !maximum_value(NULL, 0, &maximum) ? "yes" : "no");
    return 0;
}

Compile and run it. The && prevents execution if compilation fails:

gcc -std=c17 -Wall -Wextra -Werror -pedantic array-length-parameters-demo.c -o array-length-parameters-demo &&
./array-length-parameters-demo

Observed output:

Count: 4
Maximum: 19
Empty input rejected: yes

Read the result

At the declaration site, sizeof samples measures the complete array and sizeof samples[0] measures one element. Their quotient is four. The function receives a pointer and an explicit count, handles empty input before indexing, and returns success separately from the computed value.

Apply it carefully

The size formula is not a general pointer-length operation. Applying sizeof to a pointer, including a parameter declared with array syntax, measures the pointer instead. The caller must provide an accurate count for accessible elements; the null checks cannot detect a non-null pointer paired with an excessive count.

The GNU reference documents the relevant interface or language rules.

Clean up

Remove only the source and executable created here, then remove the empty practice directory:

rm -- array-length-parameters-demo.c array-length-parameters-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining contents before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on