Passing Arrays to Functions in C
In C, arrays can be passed to functions to perform operations like searching, sorting, and modifying elements. Unlike regular variables, arrays are passed by reference — the function receives the address of the first element, not a copy. This means any changes made inside the function will affect the original array.Syntax
Example: Display Array Elements
Example: Modify Array Inside a Function
Example: Find the Sum of an Array
Important Points
- Arrays are always passed to functions by reference (pointer to the first element)
- The
sizemust be passed as a separate argument — there is no built-in way to get the size inside the function - Changes made to the array inside the function do affect the original array
- To prevent modification, use the
constqualifier:void display(const int arr[], int size)