Thursday, September 22, 2011

C RETURNING AN ARRAY FROM A FUNCTION









REVISED: Sunday, March 15, 2015





CONTENTS
I.    C RETURNING AN ARRAY FROM A FUNCTION INTRODUCTION
II.   C RETURNING AN ARRAY FROM A FUNCTION
III.   RETURNING AN ARRAY FROM A FUNCTION EXAMPLE PROGRAM SOURCE CODE
IV. C RETURNING AN ARRAY FROM A FUNCTION REFERENCES

YOU WILL LEARN:
1.  C returning an array from a function.
2.  C function prototype, calling function and function definition.

I. INTRODUCTION


Welcome to the “C Returning an Array from a Function Tutorial.”

II. RETURNING AN ARRAY FROM A FUNCTION


Extensive comments are included in the sample code snippets in order to answer basic questions regarding the three ways to return a two dimensional array from a function as shown below:

A. Array To A Pointer


The array is returned from the function definition as a pointer.

1. Prototype:

int *array2Ptr();
/*
Declaration of function returning an int pointer.
*/

2. Calling Function:

one = array2Ptr();

3. Function Definition:

/*
Array To A Pointer
*/
int *array2Ptr()
{
    static 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
    */

    return *arr;
/*
Returns array arr[][2] as pointer *arr.
 */
}

B. Array To A One Dimensional Pointer Array


The array is returned from the function definition as a one dimensional pointer array.

1. Prototype:

int ( *arrayToOneDimPtrArray() )[2];
/*
Declaration of one dimensional int pointer function array.
*/

2. Calling Function:

two = arrayToOneDimPtrArray();

3. Function Definition:

/*
Array To A One Dimensional Pointer Array.
*/
int ( *arrayToOneDimPtrArray() )[2]
{
    static int arr[][2] = { { 00, 01 }, { 10, 11 }, { 20, 21 }, { 30, 31 } };
   
    return arr;
/*
Returns array arr[][2] as one dimensional pointer array arr.
*/
}

C. Array To A Two Dimensional Pointer Array


The array is returned from the function definition as a two dimensional pointer array.

1. Prototype:

int ( *arrayToTwoDimPtrArray() )[4][2];
/*
Declaration of int two dimensional pointer function array.
*/

2. Calling Function:

three = arrayToTwoDimPtrArray();

3. Function Definition:

/*
Array To A Two Dimensional Pointer Array
*/
int ( *arrayToTwoDimPtrArray() )[4][2]
{
    static int arr[][2] = { { 00, 01 }, { 10, 11 }, { 20, 21 }, { 30, 31 } };
   
    return ( int(*)[4][2] )arr;
/*
Returns array arr[][2] as two dimensional pointer array ( int(*)[4][2] )arr.
*/

}

III.  RETURNING AN ARRAY FROM A FUNCTION EXAMPLE PROGRAM SOURCE CODE

/***********************************
C  RETURNING AN ARRAY FROM A FUNCTION
***********************************/
#include<math.h>
/*
Math header file.
 */
#include<stdio.h>
/*
Standard I/O header file.
 */

#define rows 4

int main( int argc, char* argv[] )
{
/*
DECLARATIONS
 */

    int *one; 
/*
Declaration of int pointer one.
 */
    int i;
/*
Declaration of int variable i.
 */
   
    int ( *two )[2];
/*
Declaration of one dimensional int pointer array two.
 */
    int *ptr;
/*
Declaration of int pointer ptr.
 */
       
    int ( *three )[4][2];
/*
Declaration of int two dimensional pointer array three.
 */

/*
FUNCTION PROTOTYPES
 */
    int *array2Ptr();
/*
Declaration of function returning an int pointer.
 */
    int ( *arrayToOneDimPtrArray() )[2];
/*
Declaration of one dimensional int pointer function array.
 */
    int ( *arrayToTwoDimPtrArray() )[4][2];
/*
Declaration of int two dimensional pointer function array.
 */

    printf("\n\n      C POINTER TUTORIAL  PART 4\n\n");

    printf("  RETURNING AN ARRAY FROM A FUNCTION\n\n\n");

    one = array2Ptr();

    printf("  Returning a two dimensional array \n");

    printf("  from the array2Ptr() function\n");

    printf("  to a pointer.\n\n");
    for( i=0; i<rows; i++ )
    {
        printf( "  %5d %5d \n", *(one+i*2), *(one+i*2+1) );

    }
   
    two = arrayToOneDimPtrArray();

    printf("\n\n  Returning a two dimensional array\n");

    printf("  from the arrayToOneDimPtrArray() function\n");

    printf("  to a one dimension pointer arrray.\n\n");

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

        printf( "  %5d %5d \n", *ptr, *(ptr+1) );

        two++;
    }

    three = arrayToTwoDimPtrArray();

    printf("\n\n  Returning a two dimensional array\n");

    printf("  from the arrayToTwoDimPtrArray() function\n");

    printf("  to a two dimension pointer arrray.\n\n");

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

    }

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

    char z;

    z = getchar();
   
    return 0;
}

/*
FUNCTION DEFINITIONS
*/

/*
Array To A Pointer
*/
int *array2Ptr()
{
    static 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
    */

    return *arr;
/*
Returns array arr[][2] as pointer *arr.
 */
}

/*
Array To A One Dimensional Pointer Array
*/
int ( *arrayToOneDimPtrArray() )[2]
{
    static int arr[][2] = { { 00, 01 }, { 10, 11 }, { 20, 21 }, { 30, 31 } };
   
    return arr;
/*
Returns array arr[][2] as one dimensional pointer array arr.
 */
}

/*
Array To A Two Dimensional Pointer Array
*/
int ( *arrayToTwoDimPtrArray() )[4][2]
{
    static int arr[][2] = { { 00, 01 }, { 10, 11 }, { 20, 21 }, { 30, 31 } };
   
    return ( int(*)[4][2] )arr;
/*
Returns array arr[][2] as two dimensional pointer array ( int(*)[4][2] )arr.
 */

}

IV. C RETURNING AN ARRAY FROM A FUNCTION 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 WILL LEARN:
1.  C returning an array from a function.
2.  C function prototype, calling function and function definition.

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