CSE NotesCSE Notes
Simplifying Complexity

In C, arrays can be passed to functions in two ways: by passing a pointer to the array, or by passing the array by reference. Since arrays in C are inherently passed by reference (i.e., as pointers to the first element of the array), there’s no need for special syntax to pass them.

1. Passing Array to a Function:

When you pass an array to a function, you’re actually passing the address of the first element of the array, not a copy of the array. This means any changes made to the array inside the function will affect the original array.

Syntax for Passing an Array to a Function:

To pass an array to a function, you can use one of the following approaches:

1. Using Array Notation in the Function Parameter:

void function_name(int array[], int size) {
    // Function implementation
}

2. Using Pointer Notation in the Function Parameter:

void function_name(int *array, int size) {
    // Function implementation
}

Both notations are equivalent because an array in C is treated as a pointer to the first element.

Example 1: Passing an Array to a Function

Here’s a simple example where we pass an array to a function and modify its elements.

#include <stdio.h>

// Function to modify array elements
void modifyArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        arr[i] = arr[i] * 2;  // Doubling each element
    }
}

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

    printf("Array before modification:\n");
    for (int i = 0; i < size; i++) {
        printf("%d ", myArray[i]);
    }
    printf("\n");

    // Pass array to function
    modifyArray(myArray, size);

    printf("Array after modification:\n");
    for (int i = 0; i < size; i++) {
        printf("%d ", myArray[i]);
    }
    printf("\n");

    return 0;
}

Explanation:

  • Array Declaration: int myArray[] = {1, 2, 3, 4, 5}; declares an array with 5 integers.
  • Array Passing: In modifyArray(myArray, size), the array is passed by reference (actually as a pointer to the first element).
  • Function Modification: The modifyArray() function doubles each element of the array.

Output:

Array before modification:
1 2 3 4 5
Array after modification:
2 4 6 8 10

2. Passing Multi-Dimensional Arrays to Functions

You can also pass multi-dimensional arrays to functions. In C, when passing a multi-dimensional array, you need to specify the sizes of all but the first dimension.

Example 2: Passing a 2D Array to a Function

#include <stdio.h>

// Function to print a 2D array
void print2DArray(int arr[][3], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int my2DArray[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    // Pass 2D array to function
    print2DArray(my2DArray, 2);

    return 0;
}

Explanation:

  • 2D Array Declaration: int my2DArray[2][3] is a 2D array with 2 rows and 3 columns.
  • Passing 2D Array: We pass the array with the specified number of rows (in this case, 2).
  • Function Implementation: Inside the print2DArray function, we loop through each element using two loops to print the array.

Output:

1 2 3
4 5 6

3. Returning Arrays from Functions

In C, you cannot directly return an entire array from a function. However, you can return a pointer to an array or use dynamic memory allocation (malloc()/free()) to allocate memory for arrays and return it.

Example 3: Returning a Pointer to an Array (Dynamic Memory Allocation)

#include <stdio.h>
#include <stdlib.h>

// Function to create and return a dynamically allocated array
int* createArray(int size) {
    int *arr = (int *)malloc(size * sizeof(int));  // Allocate memory
    for (int i = 0; i < size; i++) {
        arr[i] = i + 1;  // Initialize array elements
    }
    return arr;  // Return pointer to the dynamically allocated array
}

int main() {
    int size = 5;
    int *arr = createArray(size);  // Get pointer to the new array

    // Print the dynamically allocated array
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Free dynamically allocated memory
    free(arr);

    return 0;
}

Explanation:

  • Dynamic Memory Allocation: The function createArray uses malloc() to allocate memory for an array and returns a pointer to the first element of the array.
  • Freeing Memory: After using the dynamically allocated array, we must free the memory using free() to avoid memory leaks.

Output:

1 2 3 4 5

4. Passing a Pointer to a Specific Array Element

Instead of passing the entire array, you can also pass a pointer to a specific element in the array.

Example 4: Passing a Pointer to a Specific Array Element

#include <stdio.h>

void modifyElement(int *p) {
    *p = 100;  // Modify the element pointed to by the pointer
}

int main() {
    int arr[] = {10, 20, 30};

    printf("Before modification: %d\n", arr[1]);
    modifyElement(&arr[1]);  // Pass the address of the second element
    printf("After modification: %d\n", arr[1]);

    return 0;
}

Explanation:

  • Pointer to Array Element: We pass &arr[1] to the function, which is a pointer to the second element of the array.
  • Modifying the Element: Inside the modifyElement function, we dereference the pointer and modify the value of the element.

Output:

Before modification: 20
After modification: 100

Conclusion:

  • In C, arrays are always passed by reference (as pointers), so any changes made to the array inside the function will affect the original array.
  • You can pass arrays to functions either by specifying the array’s name in the parameter list (which is treated as a pointer) or using pointer syntax.
  • For multi-dimensional arrays, you need to specify the size of all but the first dimension in the function’s parameters.
  • You cannot directly return an array from a function, but you can return a pointer to dynamically allocated memory.

These methods help in efficiently handling arrays within functions in C.