Is there a way to return a double array in c++
the array is int theBord[8][8]. :confused:
Printable View
Is there a way to return a double array in c++
the array is int theBord[8][8]. :confused:
There is no way to return even a simple array in C++.
Use std::vector< std::vector<int> > or create a struct that contains an array.
You can pass single dimensional arrays around with pointers like:
Unsure about two dimensional arrays, although char **argv works like this, doesn't it?? *Anyone*Code:#include <iostream>
void passing_single_array(int* array_you_passed);
int main(void)
{
int tmp_array[3];
passing_single_array(tmp_array);
std::cout << tmp_array[0] << " " << tmp_array[1] << " " << tmp_array[2] << std::endl;
return EXIT_SUCCESS;
}
void passing_single_array(int* array_you_passed)
{
array_you_passed[0] = 0;
array_you_passed[1] = 1;
array_you_passed[2] = 2;
}
But like Sang-drax said - a struct containing an array is good
> or create a struct that contains an array.
You could be setting yourself up for a lot of hidden copying of large amounts of data if you do this.
> Is there a way to return a double array in c++
Why are you doing this?
Arrays are passed as pointers, so there is no real need to also return that as a pointer.
Code:void foo ( int array[8][8] ) {
// do stuff
// this will change the array in the caller
}
int main ( ) {
int board[8][8];
foo( board );
}
Wow,
Thanks alot Salem, that'll save alot of time and effort in the future!!