This is a mirror of official site: http://jasper-net.blogspot.com/

How to pass a two dimensional array to a function (c++)

| Sunday, May 9, 2010
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;
}

Read more: Codeproject

Posted via email from jasper22's posterous

0 comments: