Simple int* to int

Hello.
i have function using int*. Im trying to get int* from SDL_Rect will this work?
"im wondering will this return int*"

int* Hero::PosReturnY()
{
int y = HeroPos.y;
return (int*)y;
}
No this will apsolutley not work, because you're returning a local scoped pointer. which actually isn't a pointer at all!
When functin return local int the int is dead, so your return will be also dead.

Here is an example that would work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int* f()
{
     return new int;
}

int main()
{

	//... somewere in the code later.

	int* pVariable = f();


	// ... somewere in the code later

	delete pVariable;

	return 0;
}

ok thanks for fast replay
Well, what you did earlier would work, it just wouldn't have done what you wanted it to do.
Topic archived. No new replies allowed.