Trouble with passing struct arrays

i think its best i explain this with an example

void LoadArray(somestruct* input, int amount) {
memcpy(pointersomewhere, input, sizeof(somestruct type)*amount);
}

somestruct* MakeArray() {
somestruct ArrayName[] = {{1, "blue"}, {2, "red"}};
return ArrayName;
}

int main() {
somestruct* somestuff = MakeArray();

somestruct backup[] = {{1, "blue"}, {2, "red"}};

somestruct* otherstuff = backup;

LoadArray(somestuff, 2);
LoadArray(otherstuff, 2);

return 0;
}

What happens is that the array passed on by otherstuff does get copied over in LoadArray but somestuff doesent, both the array pointers make it to LoadArray function and are within its contents identical but memcpy works on one but doesent on the other (with the only difference between the 2 being that one was created in a function). Super confused here, what gives?

EDIT: i think the array created within the function gets destroyed after the function finishes so i copied the contents over to a pointer that allocated some memory with new and returned that pointer instead, works now.
Last edited on
Topic archived. No new replies allowed.