Passing Arrays to Functions in C: 1D & 2D Array UDF | Unit 4 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 4
Passing Arrays to User-Defined Functions (UDF) in C
Complete Guide to Passing Individual Elements, Passing Entire Arrays by Address, 2D Matrix Parameters, and Mutability Rules
The parameter passing styles between arrays and UDFs are categorized into four structural mechanics:
1. Element-by-Value
Passes a single element like display(arr[2]). It acts as an independent primitive copy; changes inside the UDF do not affect the original array slot.
2. Base Address Passing
Passes the unindexed array name sort(arr, n). The UDF receives the base pointer; any modification alters the original array permanently.
3. Explicit Size Passing
C arrays do not convey their size across function boundaries. The length n must be passed as a companion argument to control loop traversal safely.
4. Tabular Parameters
When passing matrices matrixFunc(mat), the formal parameter requires an explicit column bound: void f(int a[][COLS]).
A complete architectural reference of syntax signatures, calling forms, and underlying memory mechanisms:
| Passing Method | Prototype & Calling Syntax | Parameter Passing Mechanism |
|---|---|---|
| Individual Element | void check(int x);Call: check(marks[i]); |
Call by Value: Copy of the element is pushed to the stack. Original element remains safe and unmodified. |
| Entire 1D Array (Sized) | void modify(int arr[], int n);Call: modify(marks, 5); |
Simulated Call by Reference: Passing marks passes &marks[0]. Array slots are directly mutable. |
| 1D Array (Pointer Notation) | void modify(int *arr, int n);Call: modify(marks, 5); |
Pointer Reference: Functionally identical to array notation; explicit pointer receives the base address. |
| Entire 2D Array (Matrix) | void show(int m[][3], int r);Call: show(matrix, 3); |
Row-Major Pointer: Column size is mandatory so the compiler can compute the row-jump offset correctly. |
- In C, copying thousands of contiguous memory blocks into a function stack frame would exhaust RAM and cause serious performance overhead.
- Instead, C automatically converts the array name in an argument list into a pointer to its first element (
&arr[0]). Thus, the UDF operates directly on the original memory block without generating a duplicate array.
1. Passing an Entire 1D Array to a Function to Double Its Elements:
#include <stdio.h>
// Prototype: arr[] receives base address, size holds element count
void doubleArray(int arr[], int size);
int main() {
int numbers[4] = {5, 10, 15, 20};
int i;
printf("Original array values:\n");
for (i = 0; i < 4; i++) {
printf("%d ", numbers[i]);
}
// Pass the entire array by name (base address) and its size
doubleArray(numbers, 4);
printf("\nAfter doubleArray() call (Values MUTATED):\n");
for (i = 0; i < 4; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
void doubleArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Directly mutates original memory in caller
}
}
2. Passing a 2D Array (Matrix) to a Function:
#include <stdio.h>
// Column size (3) MUST be specified in formal parameter
void displayMatrix(int mat[][3], int rows) {
int r, c;
for (r = 0; r < rows; r++) {
for (c = 0; c < 3; c++) {
printf("%d\t", mat[r][c]);
}
printf("\n");
}
}
int main() {
int grid[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
printf("Matrix printed via User-Defined Function:\n");
displayMatrix(grid, 2);
return 0;
}
Base Address Decay
In main(), writing the call doubleArray(numbers, 4) evaluates numbers as the base pointer &numbers[0].
Pointer Mapping to Formal Parameter
The formal parameter arr[] receives this memory address. No secondary array copy is constructed on the function's stack frame.
Direct Dereference & Mutation
Operations like arr[i] = val calculate memory offset *(arr + i), directly modifying the variable cells inside main().
const:
void printValues(const int arr[], int n);. Any attempt to modify elements inside the function will trigger a compile-time error.
doubleArray(numbers[], 4); or doubleArray(numbers[4], 4); is invalid syntax. Pass only the naked array identifier: doubleArray(numbers, 4);.
- Individual Element: Passed by value; changes inside function do not affect original array.
- Entire Array: Passed by reference (base address); alterations modify original array data.
- Calling Syntax: Use only the array name without brackets (e.g.
myFunc(arr, size);). - Formal Parameter: Can use empty brackets (
int arr[]) or pointer syntax (int *arr). - 2D Arrays: The second dimension (column bound) is strictly mandatory in the formal parameter.