C++ Return array from function

Dec 30, 2008 at 12:05pm
I am just starting off in c++ and I have function which I want to return two pieces of information from. So I make an array in the function and return it, but you cannot do that in c++ right. So instead I use a pointer to refer to the array in memory and then increment it to get the values right. I have googled this topic and still cannot get it to work. Here is my function, well method, its inside a class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

int defencesiveAttack()
{
	
	int * pointer;
	int  info[1];
	pointer = &info[0];
	
	// working out how much user will hit
	srand((unsigned int)time(0));
	info[0] = (strenght+exp)*rand()%5+1;

	// working out the experience pionts for the hit
	info[1] = (info[0]/2)-exp;



   return pointer;
}


1
2
3
4

person player1;
playerAction = player1.defencesiveAttack()


I am abit lost after this bit. Could anyone help? thanks
Dec 30, 2008 at 12:52pm
Right, this is what I've done, I hope this is the kinda thing you want.

You have you're "defencesiveAttack" function, you need this:

1
2
3
4
5
6
7
8
9
10
int* defencesiveAttack() // THIS LINE HAS CHANGED
{
int* pointer;
int info[1];
pointer=info; // THIS LINE HAS CHANGED
srand((unsigned int)time(0));
info[0] = (strenght+exp)*rand()%5+1;
info[1] = (info[0]/2)-exp;
return pointer;
}


Right now you have your pointer/array function set up, from your code:

1
2
person player1;
playerAction = player1.defencesiveAttack();


You can use:
playerAction[0]
and
playerAction[1]
to address these things you have made (info[0] & info[1])

Hope this helps.

(this is what I did to test this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int* test(int a,int b)
{
int* pointer;
int info[1];
pointer=info;
info[0]=a;
info[1]=b;
return pointer;
}
int main()
{
int* ptr=test(200,100);
printf("%d",ptr[0]);
printf("\n%d",ptr[1]);
getchar();
return 0;
}
)
Last edited on Dec 30, 2008 at 12:54pm
Dec 30, 2008 at 1:00pm
Woah. You fail, chris.

An array declared as array[n] has elements [0..n-1], so info[1] has just one element: info[0].
Then there's also the problem that declaring an array like that creates it in the stack, which means that once the current function returns, the array will be destroyed, which in turn means that the returned pointer will be pointing to an invalid location.

Here's a properly written example of how to return an array:
1
2
3
4
5
6
int *f(size_t s){
	int *ret=new int[s];
	for (size_t a=0;a<s;a++)
		ret[a]=a;
	return ret;
}
Dec 30, 2008 at 1:15pm
Thanks very much for your reply guys but I tried both ways. For chris I get a stack erorr of some kind and trying helios I get error C2440: 'return' : cannot convert from 'int' to 'int *' on the "return pointer" part.
Dec 30, 2008 at 1:29pm
You must have done something wrong. I compiles perfectly for me.
Dec 30, 2008 at 1:59pm
"Woah. You fail, chris." - a little blunt ;)
Well it compiles fine for me on GCC & creating an array of variable[1] always lets me use variable[0] & variable[1] & to make the point of myself failing, apparently, it was in the first post defined as info[1] so I thought I would keep with that.
Dec 30, 2008 at 2:02pm
Yes I did do somthing wrong.
Thanks ever so much for your help. :D
Dec 30, 2008 at 2:03pm
Yes, it will let you write past the end, but that doesn't mean that what you're doing is correct. God knows what you're overwriting when you do that.
Dec 30, 2008 at 2:23pm
Because God knows everything! =)
Topic archived. No new replies allowed.