Search topics...
C / C++ ConceptsPointers and Arraysfoundational

What happens when you pass an array to a function?

0 upvotes
Practice with AISoon

The array decays to a pointer to its first element. Inside the function, sizeof(arr) returns the size of a pointer (4 or 8 bytes), not the size of the original array. This is one of the most common C bugs — code that worked with a local array breaks when refactored into a function:

c
void process(uint8_t data[]) {
size_t len = sizeof(data); // BUG: returns sizeof(pointer), not array size
}

The fix is to always pass the array length as a separate parameter: void process(uint8_t *data, size_t len). There is no way to recover the original array size from a decayed pointer — the information is permanently lost at the function boundary.

Source: C / C++ Concepts Q&A