Passing Arrays to Functions

When working with arrays in C, you'll often need to pass them to functions for processing. Unlike regular variables, arrays have a special way of being passed to functions that's important to understand.

In C, arrays are passed by reference automatically. This means when you pass an array to a function, you're actually passing the memory address of the first element, not a copy of the entire array.

#include <stdio.h>

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = 5;
    
    printArray(numbers, size);  // Pass array to function
    return 0;
}

Different Ways to Declare Array Parameters

There are several equivalent ways to declare array parameters in function definitions:

// Method 1: Using square brackets
void function1(int arr[], int size) { }

// Method 2: Using pointer notation
void function2(int *arr, int size) { }

// Method 3: Specifying size (ignored by compiler)
void function3(int arr[10], int size) { }

All three methods above are functionally identical! The compiler treats them the same way.

Why Pass the Size Separately?

Since arrays decay to pointers when passed to functions, the function loses information about the array's size. That's why we typically pass the size as a separate parameter:

#include <stdio.h>

void modifyArray(int arr[], int size) {
    // We can modify the original array
    for (int i = 0; i < size; i++) {
        arr[i] = arr[i] * 2;  // Double each element
    }
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = 5;
    
    printf("Before: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    modifyArray(numbers, size);
    
    printf("\nAfter: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    
    return 0;
}

Working with 2D Arrays

For 2D arrays, you need to specify the number of columns in the function parameter:

#include <stdio.h>

void print2DArray(int arr[][3], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    
    print2DArray(matrix, 2);
    return 0;
}

Key Points to Remember

AspectDetails
Passing MethodArrays are passed by reference (address of first element)
Size InformationLost when passed to function, must be passed separately
ModificationsChanges made in function affect the original array
Parameter Syntaxint arr[], int *arr, and int arr[10] are equivalent
2D ArraysMust specify column size: int arr[][COLS]

Think of it This Way

Imagine you're giving someone directions to your house. Instead of drawing a complete map (copying the array), you just give them your address (passing the reference). They can visit your house and even rearrange your furniture (modify the array), but they need to know how many rooms you have (the size) because the address alone doesn't tell them that!

This reference-based approach makes array passing efficient since we don't copy large amounts of data, but it also means functions can modify the original array data.