Thursday, September 22, 2011

C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS









REVISED: Sunday, March 15, 2015





CONTENTS:
I.   C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS INTRODUCTION
II.  C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS
III. C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS EXAMPLE PROGRAM SOURCE CODE
IV. C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS REFERENCES

YOU WILL LEARN:
1.  C arrays.
2.  Four ways to pass two dimensional C arrays to functions.

I. C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS INTRODUCTION


Welcome to the “C Passing Two Dimensional Arrays To Functions Tutorial.”

II. C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS


Two things to keep in mind when working with arrays: First, an array name is a constant pointer to the first element of the array. Second, an array is the only type that can not be passed to a function by value. An array is passed by reference, by memory location address. When you pass an array to a function you are passing the base address, in other words a pointer to the first element of the array. Changes made to the array in the function will change the array value in the calling function.

The following array will be used to demonstrate passing two dimensional arrays to functions:

int arr[4][2] = { {00,01}, {10,11}, {20,21}, {30,31} };

The above is equivalent to the following:

int arr[][2] = { {00,01}, {10,11}, {20,21}, {30,31} };

    /*
                       Column  Column
                          Zero    One
        Row Zero    00      01
        Row One     10      11
        Row Two     20      21
        Row Three   30      31
    */
   
    int r;
/*
 Rows.
 */
    int c;
/*
 Columns.
 */
   

    r = 4;
/*
 Four rows.
 */
    c = 2;
/*
 Two columns.
 */



We will pass the above array to a function using each of the following four methods:

A. Array To Array


Passing a two dimensional array to a two dimensional array.

1. Prototype:


Compilers ignore variable names in prototypes.


The compiler needs the total number of parameters, the sequential order of the parameters, and the type of each parameter.


The name of the “brr array” is the address in the computer’s memory of the first element of the array. The prototype parameter list does not need to include the number of the “brr array” rows; however, it does require the number of the “brr array” columns as shown by the first parameter int brr[][2] in the following prototype.

void arrayToArray( int brr[][2], int rr, int cc );
/*
 Array to array.
 */

2. Calling Function:


Arguments arr, r, and c are passed by the function call to the function definition. The arguments are the “arr array” name arr, the number of the “arr arrayrows r, and the number of the “arr arraycolumns c.

arrayToArray( arr, r, c );
/*
 Array to array.
 */

3. Function Definition:


The parameters are input objects, identifiers, received by the function definition. The parameters are the “brr array” name brr, the number of the “brr array” rows rr, and the number of the “brr array” columns cc.

/*
 Array to array.
 rr rows, cc columns
 */
void arrayToArray( int brr[][2], int rr, int cc )
{
    int i;
   
    printf( "\n\n  Array to array function arrayToArray()\n\n" );

    for( i=0; i<rr; i++ )
    {
        printf( "     %5d  %5d \n", brr[i][0], brr[i][1] );

    }

    return;
}

B. Array To Pointer


Passing a two dimensional array to a pointer.

1. Prototype:

void arrayToPointer( int *brr, int rr, int cc );
/*
 Array to pointer.
 */

2. Calling Function:


The ( int * ) type cast is required in the calling function as shown below:

arrayToPointer ( ( int * ) arr, r, c );
/*
 Array to pointer.
 */

3. Function Definition:

/*
 Array to pointer.
 rr rows, cc columns
 */
void arrayToPointer( int *brr, int rr, int cc )
{
    int i;
   
    printf( "\n\n  Array to pointer function arrayToPointer()\n\n" );

    for( i=0; i<rr; i++ )
    {
        printf( "    %5d %5d \n", *( brr+i*2+0 ), *( brr+i*2+1 ) );

    }

    return;
}

C. Array To One Dimensional Pointer Array


Passing a two dimensional array to a one dimensional pointer array.

1. Prototype:

void arrayToOneDimPtrArray( int ( *brr )[2], int rr );
/*
 Array to one dimensional pointer array.
 */

2. Calling Function:

arrayToOneDimPtrArray( arr, r );
/*
 Array to one dimensional pointer array.
 */

3. Function Definition:

/*
 Array to one dimensional pointer array.
 */
void arrayToOneDimPtrArray( int ( *brr )[2], int rr )
{
    int i;
   
    int *prr;

    printf( "\n\n  Array to one dimensional pointer array function arrayToOneDimPtrArray()\n\n" );

    for( i=0; i<rr; i++ )
    {
        prr = ( int* )brr;
/*
 Type casting ( int* )
 */
        printf( "    %5d  %5d \n", *( prr ), *( prr+1 ) );
        brr++;
    }

    return;
}

D. Array To Two Dimensional Pointer Array


Passing a two dimensional array to a two dimensional pointer array.

1. Prototype:

void arrayToTwoDimPtrArray( int ( *brr )[4][2], int rr );
/*
 Array to two dimensional pointer array.
 */

2. Calling Function:

arrayToTwoDimPtrArray( &arr, r );         /*
 Array to two dimensional pointer array.
 */

3. Function Definition:

/*
 Array to two dimensional pointer array.
 */
void arrayToTwoDimPtrArray( int ( *brr )[4][2], int rr )  
{
    int i;
   
     printf( "\n\n  Array to two dimensional pointer array function arrayToTwoDimPtrArray()\n\n" );

    for( i=0; i<rr; i++ )
    {
        printf( "    %5d %5d \n", ( *brr )[i][0], ( *brr )[i][1] );
    }

    return;
}

III. PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS EXAMPLE PROGRAM SOURCE CODE

/***********************************
PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS
***********************************/
#include<stdio.h>
/*
 Standard I/O header file.
 */
#include<string.h>
/*
 String handling.
 */

void arrayToArray( int brr[][2], int rr, int cc );
/*
 Array to array.
 */
void arrayToPointer( int *brr, int rr, int cc );
/*
 Array to pointer.
 */
void arrayToOneDimPtrArray( int ( *brr )[2], int rr );
/*
 Array to one dimensional pointer array.
 */
void arrayToTwoDimPtrArray( int ( *brr )[4][2], int rr );
/*
 Array to two dimensional pointer array.
 */

int main( int argc, char* argv[] )
{
    int arr[][2] = { {00,01}, {10,11}, {20,21}, {30,31} };

    /*
                    Column  Column
                    Zero    One
        Row Zero    00      01
        Row One     10      11
        Row Two     20      21
        Row Three   30      31
    */
   
    int r;
/*
 Rows.
 */
    int c;
/*
 Columns.
 */
   
    r = 4;
/*
 Four rows.
 */
    c = 2;
/*
 Two columns.
 */

    arrayToArray( arr, r, c );                            /*
 Array to array.
 */
    arrayToPointer ( ( int * ) arr, r, c );            /*
 Array to pointer.
 */
    arrayToOneDimPtrArray( arr, r );             /*
 Array to one dimensional pointer array.
 */
    arrayToTwoDimPtrArray( &arr, r );         /*
 Array to two dimensional pointer array.
 */

    printf( "\n\n\n  Please press any key to EXIT." );

    char z;

    z = getchar();

    return 0;
}

/*
 Array to array
 rr rows, cc columns.
 */
void arrayToArray( int brr[][2], int rr, int cc )
{
    int i;
   
    printf( "\n\n  Array to array function arrayToArray()\n\n" );

    for( i=0; i<rr; i++ )
    {
        printf( "     %5d  %5d \n", brr[i][0], brr[i][1] );
    }

    return;
}

/*
 Array to pointer
 rr rows, cc columns.
 */
void arrayToPointer( int *brr, int rr, int cc )
{
    int i;
   
    printf( "\n\n  Array to pointer function arrayToPointer()\n\n" );

    for( i=0; i<rr; i++ )
    {
        printf( "    %5d %5d \n", *( brr+i*2+0 ), *( brr+i*2+1 ) );

    }

    return;
}

/*
 Array to one dimensional pointer array.
 */
void arrayToOneDimPtrArray( int ( *brr )[2], int rr )
{
    int i;
   
    int *prr;

    printf( "\n\n  Array to one dimensional pointer array function arrayToOneDimPtrArray()\n\n" );

    for( i=0; i<rr; i++ )
    {
        prr = ( int* )brr;
/*
 Type casting ( int* )
 */
        printf( "    %5d  %5d \n", *( prr ), *( prr+1 ) );

        brr++;
    }

    return;
}

/*
 Array to two dimensional pointer array.
 */
void arrayToTwoDimPtrArray( int ( *brr )[4][2], int rr )  
{
    int i;

     printf( "\n\n  Array to two dimensional pointer array function arrayToTwoDimPtrArray()\n\n" );

    for( i=0; i<rr; i++ )
    {
        printf( "    %5d %5d \n", ( *brr )[i][0], ( *brr )[i][1] );
    }

    return;
}

IV. C PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS REFERENCES


The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).


C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).

YOU HAVE LEARNED:
1.  C arrays.
2.  Four ways to pass two dimensional C arrays to functions.

Elcric Otto Circle





-->

-->

-->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment