Passing Array to Function in C Programming with Examples

Passing Array to Function

We know that we can pass any type of data (int, char, float, double etc) to a function for some processing. Same as we can also pass a complete array of any data type to a function for some processing.

Declaration of the function:

The only change to pass an array to a function is we putt square brackets [] after the name of the variable in parameters list in the declaration of a function. Which shows that the function will receive an array of the particular data type.
Just as if I want to write a function check which will receive an integer array then I will write
void check(int arr[]){
    printf("Array Received");
}
The prototype of a function has parameter int arr[] which shows that the function check will receive an integer array in the variable arr. And we can perform any processing on the array in the function body, we want.

Calling the function:

To call the function we only pass the name of the array, because an array name is actually the address of the first element in the array and we only need to have the address of the first element to iterate the complete array.
int main(){
    int array[5] = {1,2,3,4,5};
    check(array);
    return 0;
}

You can understand the concept more by understading the program written below

Example 1:

Passing Integer array to function to display data:
#include<stdio.h>
void printArray(int arr[]){
    int i;
    for(i=0;i<10;i++){
        printf("Value at arr[%d]: %d\n",i,arr[i]);
    }
}
int main(){
    int arr[10] = {2,1,4,2,6,7,9,5,2,1};
    printArray(arr);
    return 0;
}
Output:
Value at arr[0]: 2
Value at arr[1]: 1
Value at arr[2]: 4
Value at arr[3]: 2
Value at arr[4]: 6
Value at arr[5]: 7
Value at arr[6]: 9
Value at arr[7]: 5
Value at arr[8]: 2
Value at arr[9]: 1

In this program, the printArray function is receiving an integer array from main and printing the values of the array.

Example 2:

Passing integer array to function to calculate sum:
#include<stdio.h>
int calculateSum(int arr[]){
    int i,sum=0;
    for(i=0;i<10;i++){
        sum=sum+arr[i];
    }
    return sum;
}
int main(){
    int arr[10] = {2,1,4,2,6,7,9,5,2,1};
    int sum = calculateSum(arr);
    printf("Sum of all the value in the array is %d\n",sum);
    return 0;
}
Output:
Sum of all the value in the array is 39

In the program, the calculateSum function receives an integer array from main and after calculating sum returning the value back to main.

Example 3:

Passing character array to function to count vowels:
#include<stdio.h>
int isVowel(char ch){
    if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U'){
        return 1;
    }
    return 0;
}
void countVowels(char arr[]){
    int i,vowels=0;
    for(i=0;arr[i]!='\0';i++){
        if(isVowel(arr[i])){
            vowels++;
        }
    }
    printf("Total Vowels in the String are %d\n",vowels);
}
int main(){
    char arr[] = "We are learning how to pass an array to function";
    countVowels(arr);
    return 0;
}
Output:
Total Vowels in the String are 16

In this program, the countVowles function receives an array of characters from the main and calculate the total number of vowels in the array.

Passing 2-D Array to Function:

We can also pass two-dimensional arrays to functions just by putting another set of square brackets and the total number of the second subscript after the first set of the square bracket in the parameter list in the declaration of a function. Just as
void printArray(int arr[][3]){
//Some Code
}
The prototype of the function printArray shows that the function will receive two-dimensional integer array in the variable arr. And we can perform any processing on the array in the function body, we want.

Example 4:

Passing two-dimensional array to function:
#include<stdio.h>
void printArray(int arr[][3]){
    int i,j;
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
}
int main(){
    int arr[2][3] = {2,1,4,2,6,7};
    printArray(arr);
    return 0;

}
Output:
2 1 4

2 6 7

In this program, the printArray function is receiving a two-dimensional integer array from main and printing the values of the two-dimensional array.

Comments

Popular posts from this blog

Change your Application background in Scene Builder || JavaFX FXML Tutorial

5 Programming Languages To Learn in 2019

c/c++ Programming questions || Practice Problems for Beginners || C Programs List