I want to return an array from a function. I know it is supposed to be simple, using pointers, but I cannot get mine to work right. Only the first value is returning correctly. I think I know why, but I'm not sure how to actually use a pointer to return an array.
Here is the basic idea:
So I just want to return the values as an array to the main function.
Some thoughts
I think I should declare a global array, then create a pointer in the attack function that points to the global array. Then I initialize the 3 values I want in the global array, in the function. Then return the pointer I declared to function main.
I'm not exactly sure if I am reading the values in the array that is pointed to, correctly like this:
cout << narray[0] << endl;
cout << narray[1] << endl;
cout << narray[2] << endl;
Any tips/ideas?
Here is the basic idea:
Code:
int main()
{
#Call function attack()
int *narray = attack();
cout << narray[0] << endl;
cout << narray[1] << endl;
cout << narray[2] << endl;
}
int * attack()
{
#Do some calculations
#Calculate att1, att2, att3
int Att[] = {att1, att2, att3};
int *Ptr;
Ptr = &Att[];
Return Ptr;
}
So I just want to return the values as an array to the main function.
Some thoughts
I think I should declare a global array, then create a pointer in the attack function that points to the global array. Then I initialize the 3 values I want in the global array, in the function. Then return the pointer I declared to function main.
I'm not exactly sure if I am reading the values in the array that is pointed to, correctly like this:
cout << narray[0] << endl;
cout << narray[1] << endl;
cout << narray[2] << endl;
Any tips/ideas?