Pass Array to Functions in C
Last Updated : 07 Mar, 2025
Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.
In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whenever array is passed to a function, it decays into a pointer to its first element. However, there can be different syntax of passing the arrays as pointers.
The easiest way to pass array to a function is by defining the parameter as the undefined sized array. Let's take a look at an example:
C #include <stdio.h> // Array passed as an array without the size of its // dimension void printArr(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); } int main() { int arr[] = {1, 2, 3, 4, 5}; // Pass array to function printArr(arr, 5); return 0; }
Explanation: In this example, we pass an integer array arr to the function printArr(). In the function prototype, we define the array as the array of integers with no size information.
Here, you may also notice that we have passed the size of the array as second parameter to the function. It is recommended to pass the size of the array to the function as another parameter, otherwise we won't know how many elements to process.
The other ways of passing arrays as pointers include:
Passing as Sized Array
Similar to the above method, we can also define the size of the array while passing it to the function. But it still will be treated as pointer in the function.
C #include <stdio.h> // Array passed as an array with the size of its // dimension void printArr(int arr[5], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); } int main() { int arr[] = {1, 2, 3, 4, 5}; // Pass array to function printArr(arr, 5); return 0; }
Explanation: Here, we pass the array arr and its size size to the function printArray. The function then prints all elements of the array based on the given size.
Passing Array as Pointer Notation
Instead of using array notation arr[] in the function parameter, we can directly use pointer notation int *arr. All are equivalent, as arrays in C are treated as pointers to the first element of the array. This method is more flexible when working with dynamically allocated arrays.
C #include <stdio.h> // Array passed as an array with the size of its // dimension void printArr(int* arr, int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); } int main() { int arr[] = {1, 2, 3, 4, 5}; // Pass array to function printArr(arr, 5); return 0; }
Passing Multidimensional Arrays
Multidimensional arrays, such as 2D arrays, can also be passed to functions in C. They will also be treated as pointers but there are a few differences while passing them as they have a few extra dimensions that the compiler has to know about. Refer to this article to learn more - How to pass a 2D array as a parameter in C?
Similar Reads
C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in
7 min read
Properties of Array in C An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
8 min read
Length of Array in C The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.In C language, we don't have any pre-defin
3 min read
Multidimensional Arrays in C - 2D and 3D Arrays A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimension
8 min read
Initialization of Multidimensional Array in C In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial
4 min read
Jagged Array or Array of Arrays in C with Examples Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example:arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9},
3 min read
Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
3 min read
How to pass a 2D array as a parameter in C? A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function.The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and co
3 min read
How to pass an array by value in C ? In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
2 min read
Variable Length Arrays (VLAs) in C In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time.Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope or
2 min read