Pass 2D array to a function!!



how to pass 2D array to a function ?

e.g:

void twoDim(int values[][],int size)
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
cout << Nums[i][j] <<" ";
}
cout << endl;
}
}
.........
.................................

Is the heading correct ?
and how to call this function ?
thats fine except that for the for the last dimension there you need to specify the size. to call the function you would do something like this
1
2
3
int values[2][2] = { { 0, 1 }, { 2, 3 } };
int size = sizeof(values)/sizeof(values[0][0]);
twoDim(values, size);


please use code tags in the future by clicking the < > sign under format.
Last edited on

Sorry, OK I will next time :>
.................................................
how to solve these errors ?

error C2087: 'values' : missing subscript (in the heading of the void function)
error C2664: 'twoDi' : cannot convert parameter 1 from 'int [9][9]' to 'int [][1]' (in the calling function in main)

??
.....
Last edited on
any dimensions beyond the first, have to be explicitly defined in a function header, so

void twoDim(int values[][9],int size)

thanks both :>

it works.
Topic archived. No new replies allowed.