Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts

    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.

  • #2
    New Coder
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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
    }

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    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....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •