Friday, July 31, 2009

How do I pass an array to a function in c++?

Here's my function call.


display(consolearray


[LENGTH][WIDTH]);


here's my function


void display(char consolearray


[LENGTH][WIDTH])


{


string line;


int x=0;


int y=0;


for (i=1; i %26gt; (LENGTH*WIDTH); i++)


{


if ( x != 0 %26amp;%26amp; y != 0)


x++;


cout %26lt;%26lt; consolearray[x][y];





if (x %(LENGTH-1)==0)


{


if (y %26lt; WIDTH-1)


{


y++;


x=0;


cout %26lt;%26lt; endl;


}


}


}





}


I get this error


'display' : cannot convert parameter 1 from 'char' to 'char [][25]'


Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast





Sorry for the line breaks, yahoo doesn't


allow very long lines on here.

How do I pass an array to a function in c++?
It is easier to pass the array as a pointer and a second argument with it indicating its size. Ex:





void display(char* consolearray, int length)


{


}
Reply:call the function like 'display(consolearray)'.
Reply:When you call the function, just call "display(consolearray)". If you include the array indexes, you're just passing one element of the array - you want to pass the whole thing. (I haven't confirmed the rest of your code - but hopefully this gets you on the right track).


No comments:

Post a Comment