C Tutorial C References

C - Passing an Array to a Function



C does not allow to pass an entire array as an argument to a function. However, a pointer to an array can be passed to a function.

Consider a function called MyFunction with no return type and takes an int array called arr as argument. The following syntax can be used to achieve this:

Syntax

//method 1 - using parameter as an unsized array
void MyPrint(int arr[]) {
  statements;
}

//method 2 - using parameter as a size array
void MyPrint(int arr[5]) {
  statements;
}

//method 3 - using parameter as a pointer
void MyPrint(int* arr) {
  statements;
}

Method 1:

In the example below, the function called MyFunction1 and MyFunction2 are created which takes an array parameter as unsized array to print all elements of the array.

#include <stdio.h>

//arr[] here is a pointer even if
//square brackets are used

//accessing elements using index number
void MyFunction1(int arr[], int n) {
  printf("The array contains: ");
  for(int i=0; i<n; i++)  
    printf("%d ", arr[i]); 
}

//accessing elements using dereference operator
void MyFunction2(int arr[], int n) {
  printf("The array contains: "); 
  for(int i=0; i<n; i++) {
    printf("%d ", *arr);
    arr++;    
  }   
}

int main (){
  int MyArray[5] = {10, 20, 30, 40, 50};
  int n = sizeof(MyArray) / sizeof(MyArray[0]);

  MyFunction1(MyArray, n);
  printf("\n");
  MyFunction2(MyArray, n);
  return 0;
}

The output of the above code will be:

The array contains: 10 20 30 40 50 
The array contains: 10 20 30 40 50 


Method 2:

The same can be achieved by passing the parameter as sized array.

#include <stdio.h>
 
//arr[5] here is a pointer even if
//square brackets are used

//accessing elements using index number
void MyFunction1(int arr[5]) {
  printf("The array contains: "); 
  for(int i=0; i<5; i++)  
    printf("%d ", arr[i]); 
}

//accessing elements using dereference operator
void MyFunction2(int arr[5]) {
  printf("The array contains: "); 
  for(int i=0; i<5; i++) {
    printf("%d ", *arr);
    arr++;    
  } 
}

int main (){
  int MyArray[5] = {10, 20, 30, 40, 50}; 
  MyFunction1(MyArray);
  printf("\n");
  MyFunction2(MyArray);
  return 0;
}

The output of the above code will be:

The array contains: 10 20 30 40 50 
The array contains: 10 20 30 40 50 

Method 3:

Similarly this can be achieved by passing the parameter as pointer.

#include <stdio.h>

//accessing elements using index number
void MyFunction1(int* arr, int n) {
  printf("The array contains: ");
  for(int i=0; i<n; i++)  
    printf("%d ", arr[i]); 
}

//accessing elements using dereference operator
void MyFunction2(int* arr, int n) {
  printf("The array contains: "); 
  for(int i=0; i<n; i++) {
    printf("%d ", *arr);
    arr++;    
  } 
}

int main (){
  int MyArray[5] = {10, 20, 30, 40, 50}; 
  int n = sizeof(MyArray) / sizeof(MyArray[0]);

  MyFunction1(MyArray, n);
  printf("\n");
  MyFunction2(MyArray, n);
  return 0;
}

The output of the above code will be:

The array contains: 10 20 30 40 50 
The array contains: 10 20 30 40 50 

❮ C - Arrays