Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

How to Pass a Two Dimensional Array to a Function (C++)

2.00/5 (6 votes)
8 May 2010CPOL 59K  
How to pass a two dimensional array to a function (C++)

Introduction

Simply define a size of the array in the function header like this:

C++
void someFunc(int buff[3][2])

and make a call to the function with another two dimensional array:

C++
void add(int swap[3][2], int swap2[3][2]) 		/* Function definition 	*/
{
  int temp,i;

  for (i=0; i<3; i++)
  {
    temp       = swap[i][0];
    swap[i][0] = swap[i][1];
    swap[i][1] = temp;
  }
  return;
}

void display(int array[3][2],int array2[3][2]  ) 	/* Function definition 	*/
{
  int count=0,count1=0;

  for (count=0;count<3;count++)
    for (count1=0;count1<2;count1++)
      printf("%d ", array[count][count1]);

   printf("\n");
  puts("");
}

int main()
{
	int i[3][2]=  { {1,2}, {3,4}, {5,6}  };

  display(i,i);				/* i is a pointer	*/

  add(i,i);

  display(i,i);

  return 0;
}	

History

  • 8th May, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)