Back to C Programming
2025-12-276 min read

22.1.4.1 Array parameters are pointers (C Programming)

Learn 22.1.4.1 Array parameters are pointers (C Programming) step by step with clear examples and exercises.

Title: Array Parameters are Pointers in C Programming

Why This Matters

Understanding that array parameters in C programming are pointers is crucial for writing efficient and effective code, especially when dealing with functions that manipulate arrays. This concept is essential for interviews, real-world coding challenges, and debugging complex issues involving arrays. By mastering this topic, you'll be able to create more powerful and flexible programs.

Arrays in C are a fundamental data structure, but they can become unwieldy when dealing with large amounts of data or complex operations. Passing arrays as pointers allows functions to manipulate the original data directly, making it more efficient and flexible. It also avoids the need for copying large amounts of data when passing arrays between functions.

Prerequisites

Before diving into the core concept, you should have a good understanding of:

  1. C programming basics, including variables, operators, control structures, and functions
  2. Data structures, with a focus on arrays
  3. Pointers in C programming
  4. Basic concepts of function parameters and return values
  5. Understanding of memory allocation and deallocation in C
  6. Familiarity with common array operations such as sorting, searching, and iterating through an array

Core Concept

In C programming, when an array is passed as a function argument, the address of the first element (array base address) is passed to the function instead of the entire array. This means that array parameters are pointers! To make this clearer, let's explore how arrays and pointers in C are related:

int myArray[5] = {1, 2, 3, 4, 5}; // Declaring an array of integers named 'myArray' with 5 elements.
int *ptr = &myArray[0]; // Creating a pointer variable 'ptr' that points to the first element (base address) of 'myArray'.

In the above example, myArray is an array, and ptr is a pointer that stores the base address of myArray. This relationship demonstrates how arrays and pointers are interchangeable in C.

Array Decaying to Pointers

When an array is used as a function argument or assigned to a pointer variable, it decays into a pointer to its first element:

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

int main() {
int myArray[] = {1, 2, 3, 4, 5};
printArray(myArray); // Passing 'myArray' as an array parameter decays it into a pointer to its first element.
return 0;
}

In the printArray function, the array arr decays into a pointer to its first element when passed as a function argument. This is why we can iterate through the array using pointer arithmetic inside the function.

Array Sizes and Passing Arrays to Functions

When calling a function with an array parameter, it's essential to pass the correct size of the array so that the function knows how many elements to process. Failing to do so can lead to unexpected behavior or program crashes. To pass the size of an array, you can use another argument in the function declaration:

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])); // Passing both the array and its size to the 'printArray' function.
return 0;
}

In this example, we pass both the array myArray and its size to the printArray function so that it knows how many elements to iterate through.

Worked Example

Let's explore a worked example that demonstrates how array parameters are pointers in C programming and how they can be used for more complex operations like swapping elements.

void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1 = 5;
int num2 = 10;
int nums[] = {num1, num2}; // Creating an array of two integers named 'nums' with elements 'num1' and 'num2'.
swap(&nums[0], &nums[1]); // Passing pointers to the first and second elements of 'nums' to the 'swap' function.
printf("Num1: %d\n", num1);
printf("Num2: %d\n", num2);
return 0;
}

In this example, we define a function swap that takes two pointers to integers and swaps their values. In the main function, we create an array of two integers and pass its first and second elements as arguments to the swap function using pointer arithmetic and address-of operator (&). The function modifies the original variables by directly manipulating their addresses.

Common Mistakes

  1. ### Forgetting to pass the size of the array

When calling a function with an array parameter, it's essential to pass the correct size of the array so that the function knows how many elements to process. Failing to do so can lead to unexpected behavior or program crashes.

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

int main() {
int myArray[] = {1, 2, 3, 4, 5};
printArray(myArray); // Mistake: No size provided!
return 0;
}
  1. ### Assuming array indices start at one when using pointer arithmetic

When working with pointers and arrays, it's important to remember that array indices start at zero. This means that the first element of an array has an index of 0, not 1. Using incorrect indices can lead to off-by-one errors or other unexpected behavior.

void incrementArray(int *arr) {
for (int i = 0; i < 5; ++i) {
arr[i]++; // Mistake: Incrementing by one instead of zero!
}
}

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

Practice Questions

  1. Write a function that finds the maximum element in an array of integers using pointers.
  • Hint: Use two pointers to traverse the array and keep track of the maximum value found.
  1. Given two arrays of different sizes, write a function that swaps their elements up to the minimum size.
  • Hint: Create a temporary array with the smaller size and copy elements from both arrays into it before swapping them.
  1. Write a function that sorts an array of integers using bubble sort with pointer arithmetic.
  • Hint: Use two pointers to traverse the array, comparing adjacent elements and swapping them if necessary.
  1. Write a function that finds the second largest number in an array of integers using pointers.
  • Hint: First find the largest number, then iterate through the array again and keep track of the second largest number (excluding the largest one).
  1. Write a function that reverses an array of integers using pointers.
  • Hint: Use two pointers, one starting from the beginning of the array and another from the end, swapping elements as they meet in the middle.

FAQ

### Why do we pass arrays as pointers in C programming?

Passing arrays as pointers allows functions to manipulate the original data directly, making it more efficient and flexible. It also avoids the need for copying large amounts of data when passing arrays between functions.

### How can I pass a multidimensional array as a function argument in C programming?

To pass a multidimensional array to a function, you should pass a pointer to the first element (base address) of the array. The function can then work with the multidimensional array using pointer arithmetic. For example:

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

int main() {
int my2DArray[ROWS][COLUMNS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print2DArray(my2DArray, ROWS); // Passing 'my2DArray' as a pointer to the first element (base address) of the array.
return 0;
}

In this example, we define a function print2DArray that takes a two-dimensional array and its number of rows as arguments. The function uses pointer arithmetic to iterate through the multidimensional array.