×

Search anything:

Pass array in function in C in multiple ways

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes | Coding time: 10 minutes

An array is a collection of elements of similar data type in which the memory of all the elements are allocated in sequence.

As we know there are two ways we can pass our element in a function as a parameter that are:

  1. pass by value.
  2. pass by reference.

And guess what in case of array, its no different.

Since an array's memory get allocated in a sequence we can just pass the array to the function without worrying about anything and by this function will receive the first address of the array and from it we can access all the elements. And this way we pass the array by its reference.

Example demonstrating passing array as reference

#include<stdio.h>

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

int main(){
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    printf("Array elements: ");
  
    // Passing the array to the function
    exampleFunction(arr, 10);
    
    return 0;
}

Recieving array as a pointer variable

There's another way we can do this operation and that is receiving the parameter as a pointer variable.

#include<stdio.h>

// Receiving array as a pointer variable
void exampleFunction(int* arr, int size){
    int i;
    for(i=0;i<size;i++){
        printf("%d ", arr[i]);
    }
}

int main(){
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    printf("Array elements: ");
  
    // Passing the array to the function
    exampleFunction(arr, 10);
    
    return 0;
}

2D arrays

In case of 2D arrays there is one thing that we need to take care of and that is we need to pass the size of the column because when you create a 2D array, any type a[3][4], in memory what you actually create is 3 contiguous blocks of 4 similar type objects.

a[0][0]   a[0][1]   a[0][2]   a[0][3]   
a[1][0]   a[1][1]   a[1][2]   a[1][3]   
a[2][0]   a[2][1]   a[2][2]   a[2][3]

Now the next question is, why is that so? Because, keeping with the spec and structure of the language, any type a[3][4] actually expands out into any type (* a)[4], because arrays decay into pointers.

And in fact that also expands out into any type (* (* a)), however, you've now completely lost the size of the 2D array. So, you must help the compiler out a bit.

#include<stdio.h>

void exampleFunction(int arr[][5], int row, int col){
    int i=0;;
    int j = 2;
    for(i=0;i<row;i++){
        for(int j=0;j<col;j++){
            printf("%d ", arr[i][j]);
        }
    }
}

int main(){
    int arr[][5] = {{1,2,3,4,5}, {6,7,8,9,10}};
    printf("Array elements: ");
  
    // Passing the array to the function
    exampleFunction(arr, 2, 5);
    
    return 0;
}

To pass array elements as a value, we can pass any specific value of the array.

#include<stdio.h>

void exampleFunction(int value){
    printf("%d ", value);
}

int main(){
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    printf("Value at 6th index: ");
  
    // Passing the array to the function
    exampleFunction(arr[6]);
    
    return 0;
}
Pass array in function in C in multiple ways
Share this