# Thread: How to return two elements of an array

1. ## How to return two elements of an array

I would like to return both elements of n[].
I cant find out how to do it.

Code:
```short* GetPosition(char letter, short number)
{
short n[2] = {0,0};

if     (letter == 'a' && number == 1) n[] = {7,  3};
else if(letter == 'a' && number == 2) n[] = {7, 10};
else if(letter == 'a' && number == 3) n[] = {7, 17};
else if(letter == 'a' && number == 4) n[] = {7, 24};
else if(letter == 'a' && number == 5) n[] = {7, 31};
else if(letter == 'a' && number == 6) n[] = {7, 38};
else if(letter == 'a' && number == 7) n[] = {7, 45};
else if(letter == 'a' && number == 8) n[] = {7, 52};

return n[0, 1];
}```

2. Eh, what is n? It looks like you have incorrect syntax.

Chances are, you want to return a std::pair<int, int>, or some struct or class object.

3. That doesn't look like valid code.

4. I edited the code, changed the return value from char* to short*.

n[2] is an array of two elements.

Chances are, you want to return a std:air<int, int>, or some struct or class object.
You mean i cant return both elements of an array?

5. No, you would then be returning a pointer to the first element of a local array, which means that the caller would get a pointer that points to something that no longer exists, which is a Bad Thing. Plus, you still have invalid syntax, e.g.,
Code:
`n[] = {7,  3};`
One way of doing what you want is to #include <utility> and use std::pair<short, short> with std::make_pair, e.g.,
Code:
```std::pair<short, short> GetPosition(char letter, short number)
{
using std::make_pair;
if     (letter == 'a' && number == 1) return make_pair(7, 3);
else if(letter == 'a' && number == 2) return make_pair(7, 10);
// ...
}```

6. Thank you Laserlight!

Code:
`n[] = {7,  3};`
May i ask why would this be invalid? Im assigning values to an array.

7. Originally Posted by Ducky
May i ask why would this be invalid? Im assigning values to an array.
... which is invalid. You cannot assign to an array.

8. Arrays

"When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty . In this case, the compiler will assume a size for the array that matches the number of values included between braces

Sorry i dont get it. This is what i did.

9. Originally Posted by Ducky
Arrays

"When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty . In this case, the compiler will assume a size for the array that matches the number of values included between braces

Sorry i dont get it. This is what i did.
Nope, that was assignment, not initialization. Initialization is this line:
Code:
`short n[2] = {0,0};`

10. Originally Posted by Ducky
When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty . In this case, the compiler will assume a size for the array that matches the number of values included between braces
That's correct.

Originally Posted by Ducky
Sorry i dont get it. This is what i did.
No, you attempted to assign to an array, not to initialise an array. You did initialise n, on line 3 of your code snippet.

Originally Posted by Ducky
And can i assign only one element of
Yes, by using the first or second public member variables of the pair, e.g.,
Code:
```std::pair<short, short> position = GetPosition(c, n);
std::cout << position.first << ' ' << position.second << std::endl;```
However, you may find it better to define your own Position class instead of using std::pair.

11. Thanks to both of you!

So once an array is initialized you cannot change their elements?

12. Originally Posted by Ducky
So once an array is initialized you cannot change their elements?
No, you may be able to assign to the elements of an array.

13. Ah ok i understand. So i should have written something like this i guess.

Code:
```
if     (letter == 'a' && number == 1)
n[0] = {7} ;
n[1] = {3} ;```

14. Originally Posted by Ducky
So i should have written something like this i guess.
More like:
Code:
```n[0] = 7;
n[1] = 3;```
but then you would still have the problem where you return a pointer to an object that no longer exists by the time control returns to the caller.

15. The thing is that without other changes it would still be wrong to return n (pointer to local array).

More likely, and with other simplifications:

Code:
```//pass array in to be modified
short* GetPosition(short* n, char letter, short number)
{
n[0] = n[1] = 0;
if (letter == 'a' && number >= 1 && number <= 8) {
n[0] = 7;
n[1] = 7 * number - 4;
}
}```