Pass an Array to a Function in C

In C, we can pass an array to a function by passing its base address. Since arrays are inherently passed by reference, any modifications inside the function affect the original array. This allows functions to operate efficiently on large datasets without copying the entire array.


Examples of Passing an Array to a Function

1. Passing an Array to a Function to Print Elements

In this example, we define a function that takes an integer array and its size as arguments. The function iterates through the array and prints each element.

main.c

</>
Copy
#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 numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Passing array to function
    printArray(numbers, size);

    return 0;
}

Explanation:

  1. We define the function printArray() that takes an integer array arr[] and its size as parameters.
  2. Inside printArray(), a for loop iterates through the array and prints each element.
  3. In main(), we declare an array numbers[] and calculate its size using sizeof(numbers) / sizeof(numbers[0]).
  4. We call printArray(), passing numbers and size.
  5. The function prints the elements of the array.

Output:

1 2 3 4 5

2. Modifying an Array Inside a Function

In this example, we pass an array to a function that doubles each element. Since arrays are passed by reference, the modifications reflect in the original array.

main.c

</>
Copy
#include <stdio.h>

// Function to double each element in the array
void doubleArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        arr[i] *= 2;
    }
}

int main() {
    int numbers[] = {2, 4, 6, 8, 10};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Passing array to function
    doubleArray(numbers, size);

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

    return 0;
}

Explanation:

  1. We define the function doubleArray() that takes an integer array arr[] and its size.
  2. A for loop iterates through the array, multiplying each element by 2.
  3. In main(), we declare and initialize an array numbers[].
  4. We call doubleArray(), passing numbers and size.
  5. After returning, we print the modified array, showing that values have doubled.

Output:

4 8 12 16 20

3. Passing a Multi-Dimensional Array to a Function

In this example, we pass a 2D array to a function that prints its elements.

main.c

</>
Copy
#include <stdio.h>

// Function to print a 2D array
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}};

    // Passing 2D array to function
    print2DArray(matrix, 2);

    return 0;
}

Explanation:

  1. We define the function print2DArray() that takes a 2D array with fixed column size 3 and the number of rows.
  2. Two nested for loops iterate through the rows and columns, printing elements.
  3. In main(), we declare a 2D array matrix[][] with 2 rows and 3 columns.
  4. We call print2DArray(), passing matrix and row count 2.

Output:

1 2 3
4 5 6

Conclusion

In this tutorial, we explored different ways to pass arrays to functions in C:

  1. Passing a 1D array to print its elements.
  2. Modifying an array inside a function.
  3. Passing a 2D array to a function.

Understanding how to pass arrays to functions helps in creating reusable and efficient code.