Skip to content

Passing arrays to a function

Passing arrays to functions in C is a crucial aspect of modular programming, allowing you to reuse code and manipulate data more efficiently. When you pass an array to a function, you’re essentially passing a pointer to the first element of the array. Let’s delve into the details:

Passing Arrays to Functions:

1. Passing by Reference (Pointer):

When passing an array to a function, you often pass it by reference, meaning you pass a pointer to the first element of the array. This allows the function to access and modify the original array directly.

Example:

#include <stdio.h>

// Function to print elements of an array

void printArray(int *arr, int size) {

    for (int i = 0; i < size; i++) {

        printf(“%d “, arr[i]);

    }

    printf(“\n”);

}

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int size = sizeof(arr) / sizeof(arr[0]);

    printf(“Array elements before modification: “);

    printArray(arr, size);

    // Modifying array elements

    for (int i = 0; i < size; i++) {

        arr[i] *= 2;

    }

    printf(“Array elements after modification: “);

    printArray(arr, size);

    return 0;

}

Output:

Array elements before modification: 1 2 3 4 5

Array elements after modification: 2 4 6 8 10

In this example, printArray() function accepts a pointer to the first element of the array. The function then iterates over the array using pointer arithmetic to print each element.

2. Passing Array Size:

It’s common practice to pass the size of the array along with the array itself to the function. This helps the function know the bounds of the array and prevents potential buffer overflows.

3. Array Notation:

Alternatively, you can use array notation in function parameters to specify that the parameter is an array. However, keep in mind that array notation is essentially a pointer notation.

Example:

#include <stdio.h>

// Function to print elements of an array

void printArray(int arr[], int size) {

    for (int i = 0; i < size; i++) {

        printf(“%d “, arr[i]);

    }

    printf(“\n”);

}

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int size = sizeof(arr) / sizeof(arr[0]);

    printf(“Array elements: “);

    printArray(arr, size);

    return 0;

}

Output:

Array elements: 1 2 3 4 5

4. Dynamic Memory Allocation:

If the size of the array is not known at compile time, you can dynamically allocate memory for the array using malloc() or calloc(). In this case, you need to pass the pointer to the array along with its size to the function.

5. Limitations:

When passing arrays to functions, be cautious about the potential pitfalls, such as not knowing the array’s size inside the function (unless it’s explicitly passed), potential buffer overflows, and the inability to determine the original array’s size from a pointer alone.

Passing arrays to functions is a fundamental concept in C programming, enabling modular and reusable code. Understanding how to pass arrays correctly and efficiently is essential for writing robust and maintainable programs.