***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Posts

    Wednesday, November 18, 2020

    Passing Array to Function in C

    In C, there are various general problems which requires passing more than one variable of the same type to a function.

    Declaring Function with array as a parameter

    There are two possible ways to do:

     

    1. Have an array as a parameter.

    int sum (int arr[ ]);

     

    1. Or, have a pointer to hold the base address of array.

    int sum (int* a);



    When a formal parameter is declared is declared in a function as an array, it is interpreted as a pointer variable not as an array.

    Passing arrays as parameter to function

     1. Passing a single array element to a function

     Declare and define an array in main( ) function and pass one of the array element to a function

     Ex:

    #include<stdio.h>

     void printArray(int a);

     int main( )

    {

        int myArray[] = { 2, 3, 4 };

        printArray(myArray[2]);        //Passing array element myArray[2] only.

        return 0;

    }

     void printArray(int a)

    {

        printf("%d", a);

    }

     2. Passing a complete One-dimensional array to a function

     Only send in the name of the array as argument, which is nothing but the address of the starting element of the array, or say the starting memory address.

    Example:

     #include<stdio.h>

     float findAverage(int marks[ ]);

     void main()

    {

        float avg;

        int marks[] = {99, 90, 96, 93, 95};

        avg = findAverage(marks);       // name of the array is passed as argument.

        printf("Average marks = %.1f", avg);

    }

    float findAverage(int marks[])

    {

        int i, sum = 0;

        float avg;

        for (i = 0; i <= 4; i++)

     {

            sum += marks[i];

        }

        avg = (sum / 5);

        return avg;

    }

     3. Passing a Multi-dimensional array to a function

     Only send in the name of the array as argument, which is nothing but the address of the starting element of the array.

    Example:

     #include<stdio.h>

     void displayArray(int arr[3][3]);

     int main()

    {

        int arr[3][3], i, j;

        printf("Please enter 9 numbers for the array: \n");

        for (i = 0; i < 3; ++i)

        {

            for (j = 0; j < 3; ++j)

            {   

                scanf("%d", &arr[i][j]);

            }

        }

        // passing the array as argument

        displayArray(arr);

        return 0;

    }

     void displayArray(int arr[3][3])

    {

        int i, j;

        printf("The complete array is: \n");

        for (i = 0; i < 3; ++i)

        {

            // getting cursor to new line

            printf("\n");

            for (j = 0; j < 3; ++j)

            {      

                // \t is used to provide tab space

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

            }

        }

    }

    No comments:

    Post a Comment

    Top