how do I return an array?![]()
This is a discussion on 1 stupid question within the C++ Programming forums, part of the General Programming Boards category; how do I return an array?...
If it's a local variable, you don't. If it's dynamic memory or static memory, you can just pass a pointer to it. Three examples:
Code:char* funct() { char array[SOME_MAX_SIZE]; strcpy(array,"My Buggy message"); /* This is a bug. Don't do this in your code */ /* the storage for array does not stay in scope, so you can't return it */ return array; } static char array[SOME_MAX_SIZE]; char* funct1() { strcpy(array, "But this is okay"); return array; } /* Or... */ char* funct2() { char* array = (char*)malloc(SOME_MAX_SIZE); strcpy(array,"This is also okay"); return array; }
The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.
>how do I return an array?
Put it inside a struct, and return that. Personally I'd try not to use this method in any of my real-life designs though.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]