Open In App

Pass Array to Functions in C

Last Updated : 18 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.

In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whenever array is passed to a function, it decays into a pointer to its first element. However, there can be different syntax of passing the arrays as pointers.

The easiest way to pass array to a function is by defining the parameter as the undefined sized array. Let’s take a look at an example:

C
#include <stdio.h>

// Array passed as an array without the size of its
// dimension
void printArr(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
}

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

    // Pass array to function
    printArr(arr, 5);
    
    return 0;
}

Output
2 4 6 8 10 

Explanation: In this example, we pass an integer array arr to the function printArr(). In the function prototype, we define the array as the array of integers with no size information.

Here, you may also notice that we have passed the size of the array as second parameter to the function. It is recommended to pass the size of the array to the function as another parameter, otherwise we won’t know how many elements to process.

The other ways of passing arrays as pointers include:

Passing as Sized Array

Similar to the above method, we can also define the size of the array while passing it to the function. But it still will be treated as pointer in the function.

C
#include <stdio.h>

// Array passed as an array with the size of its
// dimension
void printArr(int arr[5], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
}

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

    // Pass array to function
    printArr(arr, 5);
    
    return 0;
}

Output
10 20 30 40 50 

Explanation: Here, we pass the array arr and its size size to the function printArray. The function then prints all elements of the array based on the given size.

Passing Array as Pointer Notation

Instead of using array notation arr[] in the function parameter, we can directly use pointer notation int *arr. All are equivalent, as arrays in C are treated as pointers to the first element of the array. This method is more flexible when working with dynamically allocated arrays.

C
#include <stdio.h>

// Array passed as an array with the size of its
// dimension
void printArr(int* arr, int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
}

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

    // Pass array to function
    printArr(arr, 5);
    
    return 0;
}

Output
11 12 13 14 15 

Passing Multidimensional Arrays

Multidimensional arrays, such as 2D arrays, can also be passed to functions in C. They will also be treated as pointers but there are a few differences while passing them as they have a few extra dimensions that the compiler has to know about. Refer to this article to learn more – How to pass a 2D array as a parameter in C?



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: