Passing array to function c – Passing Array to Function in C Programming

Passing array to function c: How to pass array to a function in C ?
In C programming, an array element or whole array can be passed to a function like any other basic data type. We can pass an array of any dimension to a function as argument. Passing an array to a function uses pass by reference, which means any change in array data inside will reflect globally.

Passing Single Element of an Array to Function
How to pass an array to a function c: We can pass one element of an array to a function like passing any other basic data type variable.

C program to pass one element of an array to function

Passing Array to Function in C Programming

#include <stdio.h>
#include <conio.h>
 
int getSquare(int num){
    return num*num;
}
 
int main(){
   int array[5]={1, 2, 3, 4, 5};
   int counter;;
   for(counter = 0; counter < 5; counter++){
       /* Passing individual array elements to function */
       printf("Square of %d is %d\n", array[counter], getSquare(array[counter]));
   }
    
   getch();
   return 0;
}

Output

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25

Passing one dimensional array to function
C passing array to function: We can pass a one dimensional array to a function by passing the base address(address of first element of an array) of the array. We can either pas the name of the array(which is equivalent to base address) or pass the address of first element of array like &array[0]. Similarly, we can pass multi dimensional array also as formal parameters.
Different ways of declaring function which takes an array as input.

  • Function argument as a pointer to the data type of array.
    int testFunction(int *array){
    /* Function body */
    } 
    
  • By specifying size of an array in function parameters.
    int testFunction(int array[10]){
    /* Function body */
    }
    
  • By passing unsized array in function parameters.
    int testFunction(int array[]){
    /* Function body */
    }
    

C Program to Pass One Dimensional Array as Parameter to a Function

C Program to Pass One Dimensional Array as Parameter to a Function

#include <stdio.h>
#include <conio.h>
 
/* This function takes integer pointer as input */
void printArrayOne(int *array, int size){
    int i;
    for(i=0; i<size; i++){
        printf("%d ", array[i]);
    }
    printf("\n");
}
 
/* This function takes sized array as input */
void printArrayTwo(int array[5], int size){
    int i;
    for(i=0; i<size; i++){
        printf("%d ", array[i]);
    }
    printf("\n");
}
 
/* This function takes unsized array as input */
void printArrayThree(int array[], int size){
    int i;
    for(i=0; i<size; i++){
        printf("%d ", array[i]);
    }
    printf("\n");
}
 
int main(){
   int array[5]={1, 2, 3, 4, 5};
   printArrayOne(array, 5);
   printArrayTwo(array, 5);
   printArrayThree(array, 5);
    
   getch();
   return 0;
}

Output

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5