I have a function that creates a 2d bool array--so the contents are either 0 or 1. Is there any way to return the array or to pass it as a pointer?
I have a function that creates a 2d bool array--so the contents are either 0 or 1. Is there any way to return the array or to pass it as a pointer?
Passing 2D array in function:
Code:bool Boo [ X ] [ Y ] = { 0, 0 };//Variable declaration Function ( bool ( * Boo ) [ Y ] ){}//Function declaration Function ( Boo );//Function call
This parameter is reserved
Return a vector instead of a pointer. Although vector<bool> is a special case where they really screwed up when creating the C++ standard (since it isn't actually a vector of bools), so according to Scott Meyers in "Effective STL" some alternatives to vector<bool> are a bitset or deque<bool>. My own idea that I wrote to him about is about using basic_string<bool>.
Anyways, you've got a lot of choices besides raw arrays.
I see, but the function in this case is being called by another function, so sending it wouldn't work.
Is there a way to just return the array without vectors?
Oh I see, I can have an array with
bool box [ X ] [ Y ] = Function ( Boo ); -- Is that what you meant?
- Sorry, I am just starting out, is there a simple way of doing this?
Last edited by Fredir; 12-01-2007 at 10:46 AM.
It is VERY unusual in C or C++ to "expect" an array to be returned from a function. The more common method of doing such things is to pass in an array to be filled by the function, e.g.
Code:void func(int arr[][10], int size) { for(int i = 0; i < size; i++) { for(j = 0; j < 10; j++) { arr[i][j] = i * j; } } } int main() { int array[100][10]; func(array, 100); return 0; }
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.