Returning a pointer - in a mess!

I am trying to create a fruit machine game and it was going very well until today.

I am trying to get the program to pick a symbol (called class Symbol) from a collection of symbols (called class Reel) and to display THAT symbol together with the one before it and the one after it - so the display would show something like this:

Symbol before middle line symbol
Middle line symbol (chosen randomly)
Symbol after middle line symbol

I have created a function that chooses a random symbol for the middle line and stores that in position 1 of an array called symbol_display. Position 0 of symbol_display is the symbol before and position 2 is the symbol after. After much testing this function works pretty well.

The problem is I now need to return a pointer to this array so my another function can then show the output onto the screen. Obviously the pointer will point to position 0 symbol_display and I can cout this onto the screen no problem. The problem comes when I try to cout symbol_display[1] and symbol_display[2]. Instead of getting the expected symbols cout is showing other symbols from the reel!

Here is a copy of the code I have so far (this is a function in class Reel):

Symbol* show_random()
	{
		srand(time(NULL));													
		int i, j, k;
		Symbol* symbol_display[3];

		j = rand() % MAX_SYMBOL;											
		//looks at the symbol before. If the symbol chosen is first in the queue then stores the address of the last symbol 
		//in the reel
		i = j - 1;															
		if (i < 0)															
		{
			i = (MAX_SYMBOL-1);
		}

		//if the symbol chosen is last in the queue then stores the address of the first symbol in the reel
		k = j + 1;
		if (k > (MAX_SYMBOL-1))
		{
			k = 0;
		}
		
		//builds the display showing the symbols chosen - stores them in an array
		symbol_display[0] = m_p_symbol[i];
		symbol_display[1] = m_p_symbol[j];
		symbol_display[2] = m_p_symbol[k];

                //These lines tested the function. It works correctly here
		cout << i << " " << j << " " << k << endl;
		cout << symbol_display[0]->symbol_name() << " " << symbol_display[1]->symbol_name() << " "<< symbol_display[2]->symbol_name() << endl;

		return *symbol_display; // returns a pointer to the array
	}


I have tested this function independently and it gives the expected results. The problem comes when I return a pointer to symbol_display which return to a third class (called class Machine) and then try to output the contents:

	void show_winlines()
		{
                        //this line prints what I expected
			cout << m_machine_contents[0].show_random()->symbol_name() << endl;

                        //These two lines do not print what is expected
			cout << ((m_machine_contents[0].show_random())+1)->symbol_name() << endl;
			cout << ((m_machine_contents[0].show_random())+2)->symbol_name() << endl;
		}
};


I suspect that the problem lies in the lines

cout << ((m_machine_contents[0].show_random())+1)->symbol_name() << endl;
			cout << ((m_machine_contents[0].show_random())+2)->symbol_name() << endl;


So if I have returned a pointer to an array from a function I know how to point to the first element of that array but how do I access the 2nd and 3rd.

I dont explain myself very well. If you have any questions please ask - it probably doesnt help that I am trying to use 3 different classes!
Last edited on
So you mean that the symbols display garbage when you return them to some other function? And display correctly when you use them in the same function?

Are you sure that the pointer is still valid when you are displaying the symbols?

Also, you have this double pointer and returning single pointer. Are you sure what are you doing when you catch this pointer in the other function?
closed account (zb0S216C)
chesterbloke wrote:
// returns a pointer to the array (sic)

2 things wrong with this. First, a function cannot return an array unless your function header looks like this (your compiler may not support it): Symbol( *show_random )( void )[ 3 ] { /* Body */ }. Second, you're returning an array that's local to show_random( ).

Wazzak
Last edited on
So you mean that the symbols display garbage when you return them to some other function? And display correctly when you use them in the same function?


No. Whats returned isnt garbage. Its returning other symbols but NOT the symbols I am expecting it to return - but the code works as I expect it to work inside the function.

2 things wrong with this. First, a function cannot return an array unless your function header looks like this (your compiler may not support it): Symbol( *show_random )( void )[ 3 ] { /* Body */ }. Second, you're returning an array that's local to show_random( ).

Wazzak


I am using Microsoft Visual C++ 2010 and dont know if its supported. If not what I attempting is fundamentally wrong. Thats cool - just back to the drawing board.

So can anyone suggest a solution or another way round it? I need a function that will choose a symbol at random from an array and be able to return that symbol plus the one before it and the one after it.
closed account (zb0S216C)
chesterbloke wrote:
So can anyone suggest a solution or another way round it?

Yes. You can optionally pass an array as an argument. This can be achieved like this:

1
2
3
4
void Function( int( &Buffer )[ n ], ... )
{
     // ...
}

[Note: n represents the size of the buffer ]

Wazzak
I agree with Framework that the array is local to the function and may not be valid after the function returns.
Thanks for everyones help but I went back to the drawing board and solved my problem a different way. As usual I was overthinking everything and making life too complicated!
Topic archived. No new replies allowed.