Return array of structures from a function

First, I learned to return just a basic struct from a function, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

struct Numbers {
    string first;
    string second;
    string third;
};

Numbers CollectSome();

int main () {
    
    Numbers output;

    output = CollectSome();

    cout << output.first << endl;
    cout << output.second << endl;
}

Numbers CollectSome() {
    Numbers input;
    input.first = "fuu";
    input.second = "foo";
    return input;
}


So it shows fuu and foo, like it meant to be.

But then I need to get some arrays in the function to the struct. This one below doesn't work. I didn't find any instructions what is the correct syntax. Can you make it to work for me? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

struct Numbers {
    string first;
    string second;
    string third;
};

Numbers CollectSome();

int main () {
    
    Numbers output[10];

    output[10] = CollectSome();

    cout << output[0].first << endl;
    cout << output[0].second << endl;
    
}

Numbers CollectSome() {
    Numbers input[10];
    input[0].first = "fuu";
    input[0].second = "foo";
    return input[10];
}
when you define an array of structs, each element will have a string first, second and third.
in your main function when youre calling your CollectSome() function youre assigning the values gathered by the function to the 10th element, but when its time to output youre outputting:
 
cout << output[0].first << endl;


when you want to be outputting
 
cout << output[10].first << endl;


secondly, in your function there is another error. your changing the values of input[0], and then returning input[10], which you havent altered yet and probably just contains some junk values. in the function you dont need to create an array of Numbers, you just need a single element. change the function back to like it was in the first example like this:
1
2
3
4
5
6
Numbers CollectSome() {
    Numbers input;
    input.first = "fuu";
    input.second = "foo";
    return input;
}


if you make these two changes it should work :)
Sorry for not being specific enough. I'm not sure if you understood this correctly.

What I meant is to collect inside the function the arrays full of strings.

Eventually, it would be something like that inside the struct.

input[0].first = "111";
input[1].first = "222";
....
input[9].first = "999";


input[0].second = "aaa";
input[1].second = "bbb";
....
input[9].second = "iii";

and then also the third one.

That is why I had return[10], but obviously that is wrong way to pass them to main program.
AFAIK you can't return arrays. Pass them to the function as a parameter.
void CollectSome(Numbers input[], size_t size);
The call
1
2
Numbers output[10];
CollectSome(output); //note that there is no [] 


Also, remember that the index goes from 0 to n-1, so array[n] is out of bounds
since all that arrays really are are pointers, if you pass them to a function they will always be passed by reference, so you can make the function void and just alter the elements of the array in the function.
Topic archived. No new replies allowed.