...

View Full Version : c++ -- returning an array from function



p4plus2
08-18-2008, 03:25 AM
Ok i have a small problem where i have a function that generates an array of characters. However, if i try to return that array i get a mess of errors. I think that i need to use a pointer to do it however, all the examples i saw used integers and did not work for me. This project i am working on is over my head, but that is how i learned all my other programing languages so please understand that my errors may just be newbie errors. Thanks! ~p4plus2~

...and if it helps here is the function:
i know i could combine the separate for loops but i left em apart since its was for a debugging reason and will not be there in my final product.
This also may not be the best way that i can accomplish what i am doing, but i think it is probably a better learning experience to do it the long way, however feel free to suggest another way to do this so if i make a new version maybe i will try a method suggested.


char start_rubik(){
char cube[6][3][3];
char color[] = {'b', 'o', 'g', 'r', 'w', 'y'};
for(int i = 0; i != 6; i++){
for(int b = 0; b != 3; b++){
for(int a = 0; a !=3; a++){
cube[i][b][a] = color[i];
}
}
}
for(int i = 0; i != 6; i++){
for(int b = 0; b != 3; b++){
for(int a = 0; a !=3; a++){
std::cout << cube[i][b][a];
}
std::cout << "\n";
}
}
return cube; //this is that nasty array i can't figure out.
}


EDIT:
For anybody who didn't guess, this script (once combined with all its other functions) will be a rubiks cube command line.....as stupid of an idea as that is i think it is a great learning experiance

crazyman
08-18-2008, 02:05 PM
Hmm i think u should pass an array by argument of function.You cant return your array cuz u have declared your array into function, it will be destroy when function complete its staments.

char start_rubik(char [6][3][3])
{
...//something code
}

oracleguy
08-18-2008, 05:47 PM
And when you pass an array as a parameter they are always passed by reference. Crazyman's suggestion is the best way to go.

p4plus2
08-18-2008, 08:23 PM
Thanks for the idea i got everything working, however i am giving up this project(for now) it is way over my head for only knowing c++ a 2 days....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum