Back to C Programming
2025-12-146 min read

22.1.4 Arrays as Parameters (C Programming)

Learn 22.1.4 Arrays as Parameters (C Programming) step by step with clear examples and exercises.

Why This Matters

Passing arrays as parameters in C programming is an essential skill that allows you to write efficient, reusable, and maintainable code. Mastering this technique will equip you with the tools necessary to tackle complex problems involving large amounts of data. By understanding how arrays are passed to functions, you'll be better prepared to handle real-world coding challenges and debug common errors.

Prerequisites

Before delving into array parameter passing, it is crucial that you have a solid foundation in the following topics:

  1. C Basics
  2. Variables and Data Types in C
  3. Functions in C
  4. Arrays in C
  5. Pointers in C

Core Concept

In C programming, arrays can be passed to functions as arguments and returned as function results using two methods: passing by value and passing by reference. Let's explore the details of each method.

Passing Arrays by Value

When an array is passed to a function by value, the function receives a copy of the array's address (a pointer to the first element). Any changes made within the function only affect the local copy and do not modify the original array. Here's an example:

void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
}

int main() {
int myArray[] = {1, 2, 3, 4, 5};
printArray(myArray, sizeof(myArray) / sizeof(myArray[0]));
return 0;
}

In this example, the printArray() function receives a copy of the myArray from the main program. The function iterates through the array and prints its elements without modifying the original data.

Passing Arrays by Reference

Passing arrays by reference allows functions to modify the original array's content. To achieve this, we use pointers to pass the address of the first element directly:

void reverseArray(int arr[], int size) {
for (int i = 0; i < size / 2; ++i) {
int temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}

int main() {
int myArray[] = {1, 2, 3, 4, 5};
reverseArray(myArray, sizeof(myArray) / sizeof(myArray[0]));
printArray(myArray, sizeof(myArray) / sizeof(myArray[0])); // Output: 5 4 3 2 1
return 0;
}

In this example, the reverseArray() function modifies the original array by swapping elements at opposite ends. This is possible because we pass the address of the first element directly using a pointer.

Worked Example

Let's create a program that finds the maximum and minimum values in an array using functions:

struct MaxMin {
int max;
int min;
};

struct MaxMin findMaxMin(int arr[], int size) {
struct MaxMin result;
result.max = arr[0];
result.min = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > result.max) {
result.max = arr[i];
}
if (arr[i] < result.min) {
result.min = arr[i];
}
}
return result;
}

int main() {
int myArray[] = {5, 2, 8, 4, 7};
struct MaxMin maxMin = findMaxMin(myArray, sizeof(myArray) / sizeof(myArray[0]));
printf("Maximum value: %d\n", maxMin.max);
printf("Minimum value: %d\n", maxMin.min); // Output: Maximum value: 8, Minimum value: 2
return 0;
}

In this example, we define a struct MaxMin to store the maximum and minimum values found in an array. The main program calls the findMaxMin() function with the given array and prints the results.

Common Mistakes

  1. Forgetting to pass the size of the array: When passing arrays as arguments, it's essential to also pass the size of the array so that the function knows how many elements to process.
  2. Not using pointers for passing arrays by reference: To modify an array within a function, we must use pointers and pass the address of the first element directly.
  3. Ignoring array bounds: Always be mindful of array boundaries when iterating through arrays or performing calculations. Accessing elements outside the array's bounds can lead to undefined behavior and security vulnerabilities.
  4. Not initializing arrays: It is important to initialize arrays before passing them as arguments to functions, especially if the function expects a specific value for each element.
  5. Confusing pointers and arrays: Remember that an array is a special type of pointer that points to its first element. When using pointers with arrays, it's essential to understand the difference between the array name (a pointer to the first element) and a separate pointer variable.
  6. Not handling edge cases: Functions that process arrays should be designed to handle empty arrays or arrays with only one element to avoid undefined behavior.
  7. Using incorrect function prototypes: Ensure that function prototypes match the actual function definition, including the number and data types of arguments.
  8. Forgetting to return values from functions: Functions that are supposed to return a value should include a return statement with the appropriate value.
  9. Not testing edge cases: Always test your functions with various input scenarios, including empty arrays, arrays with only one element, and arrays containing extreme values like minimum or maximum integers.
  10. Inconsistent naming conventions: Maintain consistent naming conventions for variables, functions, and data structures to make your code more readable and easier to understand.

Practice Questions

  1. Write a function that finds the sum of all elements in an array.
  2. Create a function that sorts an array using bubble sort.
  3. Implement a function that swaps two elements in an array given their indices.
  4. Write a function that returns the index of the first occurrence of a specific value in an array.
  5. Modify the reverseArray() function to reverse only the even-indexed elements of the array.
  6. Write a function that finds and returns the second maximum value in an array.
  7. Implement a function that multiplies all elements in an array by a given scalar value.
  8. Create a function that finds the average value of an array.
  9. Write a function that checks if an array contains any duplicate values.
  10. Modify the findMax() and findMin() functions to return both the maximum and minimum values in an array as a struct.
  11. Create a function that finds the product of all elements in an array.
  12. Write a function that returns the index of the first negative element in an array, or -1 if no negative elements are found.
  13. Implement a function that finds and returns the kth largest element in an array.
  14. Create a function that merges two sorted arrays into a single sorted array.
  15. Write a function that checks if an array is a permutation of another array.

FAQ

  1. Why do we pass arrays by value instead of by reference by default?

Passing arrays by value is more memory-safe and simpler for the compiler, as it avoids unpredictable side effects when multiple functions modify the same array. However, passing arrays by reference can be useful in specific scenarios where modifying the original data is necessary.

  1. What happens if we pass a non-array variable to a function expecting an array?

If you pass a non-array variable (such as an integer or a pointer) to a function that expects an array, you will likely encounter undefined behavior. The compiler may interpret the value as a memory address or treat it as an error.

  1. Can we return multiple values from a function using arrays?

Yes, we can return multiple values from a function by defining an array inside the function and filling its elements with the desired results. However, this approach should be used sparingly, as it can lead to code that is difficult to read and maintain.

  1. How do I pass a multidimensional array to a function?

To pass a multidimensional array to a function, you need to pass each dimension separately. For example:

void print2DArray(int arr[][MAX_COLS], int rows) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < MAX_COLS; ++j) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

In this example, we pass the first dimension (rows) as a separate argument and treat the entire multidimensional array as a one-dimensional array for the purpose of passing it to the function.