Passing Arrays to Function in C

In this tutorial, we will learn about the different ways to pass arrays to functions in C and how to use them effectively in your programs. This comprehensive guide includes examples and best practices for passing arrays to functions in C.


In C programming, it is possible to pass arrays to functions as arguments. This can be useful when we want to perform operations on an array, or when we want to pass a large amount of data to a function.

1. Passing an Array as a Pointer

One way to pass an array to a function in C is to pass a pointer to the first element of the array. For example, here's how to declare a function called print_array that takes a pointer to an array of integers as an argument:

C
void print_array(int* array, int size) {
   for (int i = 0; i < size; i++) {
      printf("%d ", array[i]);
   }
}

We can then call this function and pass an array to it like this:

C
int numbers[5] = {1, 2, 3, 4, 5};
print_array(numbers, 5);

This will print the following output to the console:

Output :

1 2 3 4 5


2. Passing an Array by Reference

Another way to pass an array to a function in C is to pass a reference to the array. To do this, we need to declare the function with a parameter that is a reference to an array. For example, here's how to declare a function called modify_array that takes a reference to an array of integers as an argument:

C
void modify_array(int (&array)[5]) {
   for (int i = 0; i < 5; i++) {
      array[i] *= 2;
   }
}

We can then call this function and pass an array to it like this:

C
int numbers[5] = {1, 2, 3, 4, 5};
modify_array(numbers);

for (int i = 0; i < 5; i++) {
   printf("%d ", numbers[i]);
}

This will print the following output to the console:

Output :

2 4 6 8 10


3. Passing an Array Using a Typedef

We can also use a typedef to define a new data type that represents an array. This can make our code more readable and easier to maintain by reducing the need to specify the size of the array in multiple places.


For example, here's how to use a typedef to define a new data type called IntArray that represents an array of integers:

C
typedef int IntArray[5];

We can then declare a function that takes an IntArray as an argument like this:

C
void print_array(IntArray array) {
   for (int i = 0; i < 5; i++) {
      printf("%d ", array[i]);
   }
}

We can call this function and pass an array to it like this:

C
IntArray numbers = {1, 2, 3, 4, 5};
print_array(numbers);

This will print the following output to the console:

Output :

1 2 3 4 5



Conclusion

There are several ways to pass arrays to functions in C, including passing a pointer to the array, passing a reference to the array, and using a typedef to define a new data type that represents an array. Each of these approaches has its own benefits and limitations, and it's important to choose the right one based on the needs of our program. Regardless of the approach we choose, it is important to ensure that we properly handle the array data in our functions to avoid errors and maintain the integrity of our program.