C POINTER TUTORIAL
PART 4
CONTENTS
• I. INTRODUCTION
• II. RETURNING AN ARRAY FROM A FUNCTION
• III. EXAMPLE PROGRAM
• IV. REFERENCES
I. INTRODUCTION
Hello; nice to meet you. Welcome to the “C Pointer Tutorial Part 4.”
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. EXAMPLE PROGRAM
/* ************************************
C POINTER TUTORIAL PART 4
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. 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).






MultiQuote


|