Victoria Ideas Back to catalog
Passing an Array to a Function in C - Learnprogramo
Aug 28, 2026 Victoria

Pass Array to C Function: Improving Functionality

Passing arrays into functions in C, also known as function parameters, is a common practice enabling functions to operate on data provided by the caller. This m...

Passing arrays into functions in C, also known as function parameters, is a common practice enabling functions to operate on data provided by the caller. This method enhances code modularity and flexibility, but it requires understanding of array decaying and function parameters.

Passing an Array to a Function in C - Learnprogramo

In C, arrays decay to pointers when passed into functions. This means the address of the first element is passed, not the array itself. However, accessing array elements from within the function doesn't directly reflect changes in the original array.

CODING MASTERY: THE ESSENTIAL GUIDE TO PASSING ARRAYS IN C FUNCTIONS

Understanding Array Decay

When an array is declared as a function's parameter, it's treated as a pointer to its first element. This 'decaying' behaviour occurs because function parameters in C are predefined as pointers by default.

Pass Array to Functions in C - GeeksforGeeks

For instance:

void printArray(int *arr, int n) {
    for(int i=0; i

Decay to Pointer

PASS Array as Function Parameter Like a PRO in C++

In the above `printArray` function, `arr` is treated as a pointer to the first element. The size `n` is necessary to comprehend the array's complete size.

Calling this function with an array like so:

int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5);

Will correctly print array elements.

an image of a computer screen with some text on it and the same line of code

Array Changes Inside Function

Despite the fact that the array decays to a pointer, modifications within the function don't reflect back to the original array:

void modifyArray(int *arr, int n) {
    arr[0] = 10;
}

If you call this function with `arr[5]` and then print `arr`, it will still display the original first element, not the changed one.

Passing Array to Function in C/C++

Passing Arrays by Reference

To make changes within a function reflect back to the original array, one must use pointers or dynamically allocated arrays.

How to pass array as argument in function | tutorial 22|Learn c++
a screenshot of a computer screen with some type of program in the bottom right corner
a row of blue lockers with the words c in front of it and an image of
Print reverse of array| passing array into function using pointer [Part 04]
C++ Array as Function Parameter | Passing Arrays to Functions | Lecture 36
Array Data Structure - GeeksforGeeks
Array vs Pointer in C 😳 (Simple Beginner Guide)
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
functions in c

We can create an array with `malloc`, or use advanced features like arrays of pointers or multi-dimensional arrays to pass arrays by reference:

Using malloc

Allocate memory dynamically, modify, and then free it:

int *arr = malloc(5 * sizeof(int));
modifyArr(&arr, 5);
free(arr);

Arrays of Pointers

Treating arrays like arrays of pointers and changing the structure within the function:

void modifyArray(int **arr, int n) {
    arr[0] = 10;
}

In conclusion, understanding how arrays decay to pointers and behave within functions is vital for C programming. With careful implementation, you can retain the benefits of passing arrays while mitigating potential complexities and pitfalls.

More shots

Passing an Array to a Function in C - Learnprogramo
Passing an Array to a Function in C - Learnprogramo
CODING MASTERY: THE ESSENTIAL GUIDE TO PASSING ARRAYS IN C FUNCTIONS
CODING MASTERY: THE ESSENTIAL GUIDE TO PASSING ARRAYS IN C FUNCTIONS
Pass Array to Functions in C - GeeksforGeeks
Pass Array to Functions in C - GeeksforGeeks
PASS Array as Function Parameter Like a PRO in C++
PASS Array as Function Parameter Like a PRO in C++
an image of a computer screen with some text on it and the same line of code
an image of a computer screen with some text on it and the same line of code
Passing Array to Function in C/C++
Passing Array to Function in C/C++
How to pass array as argument in function | tutorial 22|Learn c++
How to pass array as argument in function | tutorial 22|Learn c++
a screenshot of a computer screen with some type of program in the bottom right corner
a screenshot of a computer screen with some type of program in the bottom right corner

Related topics