Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2008, 02:25 AM   PM User | #1
p4plus2
Regular Coder

 
Join Date: Mar 2008
Posts: 103
Thanks: 1
Thanked 8 Times in 8 Posts
p4plus2 is an unknown quantity at this point
c++ -- returning an array from function

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.
Code:
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

Last edited by p4plus2; 08-18-2008 at 02:27 AM..
p4plus2 is offline   Reply With Quote
Old 08-18-2008, 01:05 PM   PM User | #2
crazyman
New Coder

 
Join Date: Jan 2008
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
crazyman is an unknown quantity at this point
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.
Code:
char start_rubik(char [6][3][3])
{
...//something code
}
crazyman is offline   Reply With Quote
Old 08-18-2008, 04:47 PM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
And when you pass an array as a parameter they are always passed by reference. Crazyman's suggestion is the best way to go.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 08-18-2008, 07:23 PM   PM User | #4
p4plus2
Regular Coder

 
Join Date: Mar 2008
Posts: 103
Thanks: 1
Thanked 8 Times in 8 Posts
p4plus2 is an unknown quantity at this point
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....
p4plus2 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:48 PM.


Advertisement
Log in to turn off these ads.