Click here to Skip to main content
Licence CPOL
First Posted 8 May 2010
Views 7,675
Bookmarked 3 times

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

By | 8 May 2010 | Article
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:

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

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

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)

About the Author

TopCoder23



Sweden Sweden

Member

About me:
I attended programming School and I have a degree in three programming languages.
C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.
 
I've written about a dozen small simple applications and games.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTerrible PinmemberMember 15069666:56 7 Jun '10  
GeneralMy vote of 1 PinmemberAli BaderEddin12:44 8 May '10  
GeneralRe: My vote of 1 PinmemberSledgeHammer017:48 9 May '10  
GeneralMy vote of 1 PinmemberMichel Renaud8:00 8 May '10  
GeneralMy vote of 1 PinmemberSledgeHammer017:20 8 May '10  
GeneralRe: My vote of 1 PinmemberAli BaderEddin12:41 8 May '10  
GeneralRe: My vote of 1 PinmemberSledgeHammer017:47 9 May '10  
GeneralRe: My vote of 1 PinmemberAli BaderEddin9:02 10 May '10  
Generalyou godda be kidding PinmemberJohnWallis424:07 8 May '10  
Questionthis is a joke, right? Pinmemberxliqz2:38 8 May '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 8 May 2010
Article Copyright 2010 by TopCoder23
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid