Page 1 of 1

C POINTER TUTORIAL PART 3 PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 30 June 2010 - 01:30 AM

C POINTER TUTORIAL
PART 3


CONTENTS
• I. INTRODUCTION
• II. PASSING TWO DIMENSIONAL ARRAYS TO FUNCTIONS
• III. EXAMPLE PROGRAM
• IV. REFERENCES


I. INTRODUCTION

Hello; nice to meet you. Welcome to the “C Pointer Tutorial Part 3.”

II. 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 array” rows r, and the number of the “arr array” columns 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. EXAMPLE PROGRAM

/* ********************************************
   C POINTER TUTORIAL PART 3
   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. 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).


Is This A Good Question/Topic? 1
  • +

Page 1 of 1