C Programs Tutorials | IT Developer
IT Developer

C Programming - C Passing Arrays to Functions



Share with a Friend

C Programming - C Passing Arrays to Functions

Passing Arrays to Functions in C

In C, arrays can be passed to functions either by reference or pointer. Since arrays are always passed by reference in C, when you pass an array to a function, you're passing the address of its first element. This means that changes made to the array inside the function affect the original array.

  1. Passing Arrays by Reference (Using Array Name)

When you pass an array to a function, you're essentially passing the base address (the address of the first element of the array) to the function. This means the function can access and modify the original array.

Syntax:

C

return_type function_name(data_type array_name[], int size);

The size of the array is not passed automatically, so you should either define a separate argument to specify the size of the array or use a sentinel value to indicate the end of the array.

Example:

C

#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 arr[] = {1, 2, 3, 4, 5};

    int size = sizeof(arr) / sizeof(arr[0]);  // Calculate the size of the array

    printArray(arr, size);  // Passing the array to the function

    return 0;

}

Explanation:

  • The arr[] in the function parameter means that the array is being passed by reference. The function accesses and prints the original array.
  • The size of the array is calculated using sizeof(arr) / sizeof(arr[0]) to determine the number of elements.

Output:

1 2 3 4 5
  1. Passing Arrays Using Pointers

Arrays are internally treated as pointers in C, and you can explicitly pass the array as a pointer to the function. This is equivalent to passing the array by reference, but with the pointer explicitly used to access the array.

Syntax:

C

return_type function_name(data_type *array_name, int size);

Example:

C

#include <stdio.h>

void modifyArray(int *arr, int size) {

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

        arr[i] = arr[i] * 2;  // Modify each element by multiplying it by 2

    }

}

int main() {

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

    int size = sizeof(arr) / sizeof(arr[0]);  // Calculate the size of the array

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

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

        printf("%d ", arr[i]);  // Print the modified array

    }

    return 0;

}

Explanation:

  • The function modifyArray receives a pointer to the array and modifies the array by multiplying each element by 2.
  • The array is passed directly to the function, and the changes affect the original array because it's passed by reference.

Output:

2 4 6 8 10
  1. Passing Multidimensional Arrays to Functions

Multidimensional arrays can also be passed to functions in a similar way, but you need to specify the size of each dimension except the first one. This is because the compiler needs to know how to access elements beyond the first dimension.

Syntax for 2D Array:

C

return_type function_name(data_type array_name[][size2], int size1, int size2);

Example:

C

#include <stdio.h>

void print2DArray(int arr[][3], int rows) {  // 3 is the number of columns

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

        for (int j = 0; j < 3; j++) {  // 3 is the number of columns

            printf("%d ", arr[i][j]);

        }

        printf("\n");

    }

}

int main() {

    int arr[2][3] = {

        {1, 2, 3},

        {4, 5, 6}

    };

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

    return 0;

}

Explanation:

  • The arr[][3] parameter in the function means that the number of columns (3) is known, and the rows can be passed as an argument.
  • The function prints each element of the 2D array.

Output:

1 2 3

4 5 6

  1. Important Points
  • Size of Arrays: When passing arrays to functions, the size of the array is not passed automatically. If you need to know the size of the array, you must either pass the size explicitly or use some sentinel value (e.g., -1, 0, etc.).
  • Passing Entire Arrays: In C, when an array is passed to a function, what is actually passed is a pointer to the first element of the array. Hence, modifying the array inside the function will affect the original array.
  • Multidimensional Arrays: When passing a multidimensional array, the size of the second and subsequent dimensions must be provided, as the array is stored in a contiguous block of memory.

Summary

  • Arrays in C are always passed by reference, which means changes made to the array inside a function will affect the original array.
  • You can pass arrays either using the array name (which implicitly means passing a pointer) or explicitly as a pointer.
  • When passing a multidimensional array, the size of all but the first dimension must be specified for proper access inside the function.