StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Passing arrays to functions
Download
  1. C Programming
  2. Unit 3: Modular Programming with Functions, Arrays & Recursion
Call by Reference : Recursion
Unit 3: Modular Programming with Functions, Arrays & Recursion

1. The Core Concept: Passing a Pointer

This is the most important rule to remember:

In C, you do not pass the entire array to a function. It is too inefficient to copy potentially thousands of elements.

Instead, when you pass an array name as an argument, you are just passing a pointer to the array's first element. The function receives the memory address of where the array starts.

Because the function gets the address of the original array, this is a form of Call by Reference. Any changes the function makes to the array elements will affect the original array in main().

' element in main()'s memory.]

2. Passing 1-D Arrays

A. The Syntax

When you declare a function that accepts a 1-D array, you have three equivalent options for the parameter. Let's assume you want to pass an int array.

1.  void myFunction(int arr[]): This is the most common and readable way. The empty brackets [] tell the compiler that arr is a pointer.

2.  void myFunction(int *arr): This is what is *actually* happening. It explicitly declares arr as a pointer to an integer. This is 100% identical to the first option.

3.  void myFunction(int arr[5]): You can put a size in the brackets, but the compiler ignores it. This is misleading and should be avoided.

B. The "Size" Problem

Because the function only receives a pointer to the start of the array, it has no idea where the array ends. It doesn't know if the array has 5 elements or 5,000.

CRITICAL: You must pass the size of the array as a separate argument to the function. If you don't, the function has no way to safely loop through the array and will likely read or write to invalid memory.

C. Example: Modifying a 1-D Array

This example shows two functions: one to print an array and one to modify it (by doubling its values). Notice how the changes made in doubleElements are permanent and visible back in main.

/*

 * Example: Passing a 1-D Array to functions.

 * We pass the array (a pointer) AND its size.

 */

#include <stdio.h>

 
// We must also pass the size!

void printArray(int arr[], int size);

void doubleElements(int *arr, int size); // Using pointer syntax, just to show it's identical

 
int main() {

    int numbers[5] = {10, 20, 30, 40, 50};

    int size = 5;

 
    printf("Original array in main:\n");

    printArray(numbers, size);

 
    // Pass the array to be modified.

    doubleElements(numbers, size);

 
    printf("\nArray in main after modification:\n");

    printArray(numbers, size); // The original array is changed!

 
    return 0;

}

 
/*

 * This function just reads the array.

 * 'arr' is a pointer to the start of 'numbers'.

 * 'size' is a copy of the 'size' variable from main.

 */

void printArray(int arr[], int size) {

    for (int i = 0; i < size; i++) {

        printf("%d  ", arr[i]);

    }

    printf("\n");

}

 
/*

 * This function WRITES to the array.

 * Because 'arr' points to the original 'numbers' array,

 * this function is modifying 'numbers' directly.

 */

void doubleElements(int *arr, int size) {

    for (int i = 0; i < size; i++) {

        arr[i] = arr[i] * 2; // Modifying the original array

    }

}

3. Passing 2-D Arrays

Passing 2-D arrays is trickier. The principle is the same (you pass a pointer), but the syntax is different.

A. The Syntax

When passing a 2-D array, you MUST specify the size of all dimensions except the first one.

If you have int matrix[3][4]; in main:

·         Correct: void myFunction(int arr[3][4])

·         Correct: void myFunction(int arr[][4])

·         WRONG: void myFunction(int arr[3][])

·         WRONG: void myFunction(int arr[][])

B. Why is the Column Size Required?

The compiler needs the column size (e.g., 4) to know how to find the next row. To access arr[i][j], the compiler calculates the memory address as:

Address = (start_address) + (i * NUM_COLUMNS * sizeof(int)) + (j * sizeof(int))

Without knowing NUM_COLUMNS, this calculation is impossible. The first dimension (number of rows) isn't needed for this, just for bounds checking.

C. Example: Printing a 2-D Array

/*

 * Example: Passing a 2-D Array to a function.

 */

#include <stdio.h>

 
// Define constants for the dimensions

#define ROWS 3

#define COLS 4

 
// Note: We MUST specify the column size.

// We pass the row size as a separate parameter.

void printMatrix(int arr[][COLS], int rows);

 
int main() {

    int matrix[ROWS][COLS] = {

        {1, 2, 3, 4},

        {5, 6, 7, 8},

        {9, 10, 11, 12}

    };

 
    // Pass the 2D array and its row count

    printMatrix(matrix, ROWS);

 
    return 0;

}

 
/*

 * This function can now correctly calculate

 * the location of arr[i][j] because it knows COLS.

 */

void printMatrix(int arr[][COLS], int rows) {

    printf("Printing Matrix:\n");

    for (int i = 0; i < rows; i++) {

        for (int j = 0; j < COLS; j++) {

            printf("%d\t", arr[i][j]);

        }

        printf("\n");

    }

}

 

Call by Reference Recursion
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website