C Programming - Beginner to Brilliance / Arrays and Strings

Passing Arrays to Functions

Why Pass Arrays to Functions?

You often want to pass an array to a function so that you can:

  • πŸ” Process or modify its elements (like sum, sort, reverse)

  • 🧱 Break logic into reusable blocks

  • 🧠 Avoid rewriting the same logic multiple times


⚠️ IMPORTANT CONCEPT

In C, arrays are always passed to functions by reference.

This means:

  • You do not copy the entire array, just pass the memory address of the first element.

  • βœ… So, any modification inside the function reflects in the original array.


βœ… Syntax for Passing Arrays

πŸ“Œ Function Declaration

void myFunction(int arr[], int size);

or

void myFunction(int *arr, int size);  // Equivalent

int arr[] and int *arr mean the same here β€” a pointer to the first element.


πŸ”§ Basic Example – Print Array Elements

#include <stdio.h>

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

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int length = sizeof(numbers)/sizeof(numbers[0]);

    printArray(numbers, length);
    return 0;
}
1 2 3 4 5

πŸ”„ Modify Array Elements Inside Function

This C program demonstrates passing an array to a function and modifying its contents using call-by-reference (via pointer semantics).

void doubleElements(int arr[], int size) {
    for(int i = 0; i < size; i++) {
        arr[i] *= 2;
    }
}

int main() {
    int data[] = {5, 10, 15};
    doubleElements(data, 3);

    for(int i = 0; i < 3; i++) {
        printf("%d ", data[i]);  // Output: 10 20 30
    }
    return 0;
}
10 20 30

βœ… What it does:

  • It takes two parameters:

    • arr[]: an array of integers (which is actually passed as a pointer in C)

    • size: number of elements in the array

  • It doubles every element in the array using a loop:

    • arr[i] *= 2; is shorthand for arr[i] = arr[i] * 2;

🧠 What happened?

  • arr[i] *= 2; inside the function directly modifies the original array data[].

  • 🎯 Important: Because arrays are passed by reference, not by value.


🎯 Key Point: Arrays Don't Need to Be Returned

#include <stdio.h>

void modifyArray(int arr[], int size) {
    for(int i = 0; i < size; i++) {
        arr[i] = arr[i] * 10;  // Multiply each element by 10
    }
}

int main() {
    int arr[] = {1, 2, 3};  // Original array
    int size = 3;

    // Pass array to function
    modifyArray(arr, size);

    // Print modified array
    for(int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}
10 20 30

Unlike int or float, you don’t need to return the array from the function β€” changes persist automatically.


πŸ“¦ Passing 2D Arrays to Functions

πŸ“Œ Syntax:

void printMatrix(int arr[2][3]) {
    // or int arr[][3]
}

❗ You must specify column size in 2D arrays

void printMatrix(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}};
    printMatrix(matrix, 2);
    return 0;
}
1 2 3
4 5 6

Instead of arrays, you can also use pointer notation:

void printArray(int *arr, int size);

You access elements like arr[i], which works the same as with array notation.


❌ Common Mistakes to Avoid

Mistake

Why it’s wrong

Forgetting array size

The function won’t know where array ends

Modifying without intention

Changes reflect in original array

Using return arr[]

Not needed β€” arrays are already modified


🧠 Behind the Scenes: What's Really Happening?

When you pass an array:

int arr[] = {1, 2, 3};
func(arr, 3);

C actually does this:

func(&arr[0], 3);  // address of first element

The function receives a pointer to the first element β€” not the whole array.