C++ Return Array of Structs

arrays, c, c++, return, struct

Solution

This is how I would do it.

typedef struct name
{
    string thing1;
    string thing2;
    int thing3;
    int thing4;
};

name** getNames(size_t count) {
    size_t i;
    name** names = malloc(count * sizeof(*names));
    for(i = 0; i < count; i++) {
        names[i] = malloc(sizeof(**names));
        names[i]->thing1 = "foobar";
    }
    return names;
}

Edit: I just noticed this is about c++, so the other answer is probably better.

Problem

Ok so I have a struct listed as such: ``` typedef struct name { string thing1; string thing2; int thing3; int thing4; }; ``` I use a function that runs through data and assigns everything into an array of structs establish the inside struct array. name structname; function runs fine and assigns everything inside correctly... `structname[i].thing1 structname[i].thing2` etc. will cout fine inside the function My question is how do I assign the function to return this array of structs? I can't seem to use pointers to do so and I have looked extensively on the web to find an answer. EDIT: First off, first post here. always been using the resources, but i can't stress again how helpful in learning php, c++, etc. y'all have been. I can pass structs into the function fine using a pointer, it's just returning an array of structs which seems to be the issue. My function is set up as a void so I have been using the void function(struct name &input) but that clearly doesn't seem to modify the struct. I have also tried to use the function as a return type but it mismatches as it is an array and not a struct.

Original source