C Pointers passing arrays to functions


Passing arrays to functions using pointers in C is a common technique that allows functions to operate on the data contained within arrays. This approach provides a means to modify the original array from within the function and also reduces the overhead of copying large arrays. Here's a detailed explanation, along with examples.

1. Basic Concept

When you pass an array to a function, what you're actually passing is a pointer to the first element of the array. This means that any changes made to the array elements within the function affect the original array outside the function.

2. Function Declaration

When declaring a function that takes an array as an argument, you can define the parameter as a pointer. The syntax can be as follows:

void functionName(int *array, int size);

3. Example: Passing an Array to a Function

Here's a simple example that demonstrates how to pass an array to a function using pointers:

Code Example

#include <stdio.h> // Function to modify the array void modifyArray(int *arr, int size) { for (int i = 0; i < size; i++) { arr[i] += 10; // Increment each element by 10 } } int main() { int myArray[] = {1, 2, 3, 4, 5}; int size = sizeof(myArray) / sizeof(myArray[0]); // Calculate the number of elements printf("Before modification:\n"); for (int i = 0; i < size; i++) { printf("%d ", myArray[i]); } // Pass the array to the function modifyArray(myArray, size); printf("\nAfter modification:\n"); for (int i = 0; i < size; i++) { printf("%d ", myArray[i]); // Outputs the modified array } return 0; }

Output

Before modification: 1 2 3 4 5 After modification: 11 12 13 14 15

4. Detailed Explanation

  1. Function Definition:

    • The function modifyArray takes two parameters: a pointer to an integer (int *arr) and an integer representing the size of the array (int size).
    • Inside the function, you can access and modify the elements of the array using the pointer notation.
  2. Main Function:

    • An array myArray is declared and initialized.
    • The size of the array is calculated using sizeof(myArray) / sizeof(myArray[0]).
    • The original array is printed before the modification.
    • The modifyArray function is called with myArray and its size as arguments.
  3. Modification:

    • Inside the modifyArray function, the elements of the array are modified by adding 10 to each element.
    • Since the function receives a pointer to the original array, any changes made within the function are reflected in myArray in the main function.

5. Alternative Syntax

You can also pass a multi-dimensional array using pointers. Here’s an example of passing a 2D array to a function:

Example: Passing a 2D Array

#include <stdio.h> #define ROWS 2 #define COLS 3 // Function to print a 2D array void printMatrix(int (*matrix)[COLS], int rows) { for (int i = 0; i < rows; i++) { for (int j = 0; j < COLS; j++) { printf("%d ", matrix[i][j]); // Accessing elements using array notation } printf("\n"); } } int main() { int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}}; printf("Matrix:\n"); printMatrix(matrix, ROWS); // Passing a 2D array to the function return 0; }

Output

Matrix: 1 2 3 4 5 6

6. Key Points to Remember

  • Pointer Arithmetic: Within the function, you can use pointer arithmetic to access and modify array elements.
  • Size Parameter: It is a good practice to pass the size of the array as a separate argument to avoid out-of-bounds access.
  • Modification: Changes made to the array inside the function will affect the original array, as the function works with a pointer to the first element of the array.
  • Multi-dimensional Arrays: The syntax for passing multi-dimensional arrays requires you to specify the size of all but the first dimension.

Summary

Passing arrays to functions using pointers is a powerful feature in C that allows for efficient data manipulation without the need for copying large datasets. Understanding this mechanism is fundamental for effective programming in C, enabling you to write flexible and efficient functions that can handle various data types and structures.