Passing array to function in C language

We can pass a variable as an argument of function. Like other variable we can also pass an array to function. In this article we will see how to do that.

If you don’t know about function then we will highly recommend you to learn function in C then come here to learn array to function. You should also have a good idea about array in C programming. Now lets get started to learn passing array to a function.

Main syntax

Consider the following syntax to pass an array to a function as an argument.

// Syntax for passing array to function

return_type function_name(array_name);

Method of passing an array to function

We can use any one of the following methods for passing an array to function. You can use any one of these.

// Method of passing an array to function

returnType functionName(arrayType arrayName[])    // first method

returnType functionName(arrayType arrayName[size])    // second method

returnType functionName(arrayType *arrayName)    // third method

C program to pass an array to function

In this program we will pass an array to a function and find the minimum number among the element of array. Let’s consider the example bellow.

// C program to pass an array to function
// Find minimum integer of an array
#include<stdio.h>
int minNum(int arr[], int size){
  int m = arr[0];
  int j = 0;
  for(j = 1; j < size; j++){
    if(m > arr[j]){
    m = arr[j];
    }
  }
return m;
}

int main(){
  int c = 0, min = 0;
  int number[] = {2,5,6,3,9,8};

  min = minNum(number,6);
  printf("Minimum number is = %d \n",min);
return 0;
}

Output of this array to function program

Array to function using call by value method in C

Here we will use function call by value method to pass the array to function.

// Array to function using call by value method in C
#include <stdio.h>

void printCharacters( char ch){   // function to print character
   printf("%c ", ch);
}

int main(){
   char charArray[] = {'a', 'b', 'c', 'x', 'y', 'z'};
   for (int i = 0; i < 6; i++){
       printCharacters (charArray[i]);  // pass all character to function
   }
   return 0;
}

Output of this program

array to function using call by value

Array to function using call by reference method in C

Here we will use function call by reference method to pass the array to a function.

// Array to function using call by reference method in C
#include <stdio.h>

void displayNumbers( int *nums){
  printf("%d ", *nums);
}

int main(){
  int numArray[] = {1, 2, 3, 4, 5};
  for (int x = 0; x < 5; x++){
    displayNumbers (&numArray[x]);
  }
return 0;
}

See output like this

array to function using call by reference

Example programs of array to function

Now we will see some example program where we pass an array to a function. You will get a better idea from them.

Sorting an array using C function

We have used bubble sort here to sort the array.

// Sorting an array using bubble sort in by C function
#include<stdio.h>

void sortFunction(int arr[]){   // function
int x, y, temp;
  for(x = 0; x < 10; x++){
    for(y = x + 1; y < 10; y++){
      if(arr[y] < arr[x]){
        temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
      }
    }
  }
  printf("Array after sorting is : \n");
  for(x = 0; x < 10; x++){
  printf("%d ",arr[x]);
  }
}

int main(){
  int myArray[10] = { 15, 91, 17, 10, 83, 34, 112, 87, 94, 23};
  sortFunction(myArray);
  return 0;
}

When you compile and run this program you will get the following output.

Array after sorting is :
10 15 17 23 34 83 87 91 94 112

Return array from the function

// Return array from the function
#include<stdio.h>

int* returnArray(int arr[]){
int c, d, temp;
  for(c = 0; c < 5; c++){
    for(d = c + 1; d < 5; d++){
        if(arr[d] < arr[c]){
            temp = arr[c];
            arr[c] = arr[d];
            arr[d] = temp;
        }
    }
  }
return arr;   // returning the array
}


void main(){
  int myArr[5] = {9, 101, 44, 78, 23};
  int *ptr = returnArray(myArr), k;
  printf("Array after sorting is as follows .... \n");
  for(k = 0; k < 5; k++){
    printf("%d ",*(ptr + k));
  }
}

Output of this returning array from the function program is as follows.

Array after sorting is as follows ....
9 23 44 78 101

Passing a multidimensional arrays to function

// Passing a multidimensional arrays to function
#include <stdio.h>

void printNumber(int num[2][3]){
  printf("\nDisplaying the array : \n");
  for (int c = 0; c < 2; ++c){
    for (int d = 0; d < 3; ++d){
        printf("%d\n", num[c][d]);
    }
  }
}

int main(){
  int mulArr[2][3];
  printf("Enter 6 int array element here : \n");
  for (int c = 0; c < 2; ++c){
    for (int d = 0; d < 3; ++d){
        scanf("%d", &mulArr[c][d]);
    }
  }
  printNumber(mulArr);
  return 0;
}

See the output like this

Enter 6 int array element here :
6
4
12
8
34
21

Displaying the array :
6
4
12
8
34
21

Previous pagePointer to array in C

Next pageString in C