Bellow is a simple program that works fine. It contains a function that is able to return a string of arbitrary size. The size of which is determined by the function input.
#include <iostream>
using namespace std;
string strFunc(int a){
string toBeReturned;
for(int i=0; i < a; i++){
toBeReturned += '!';
}
return toBeReturned;
}
int main(){
int x = 5;
cout << strFunc(x) << endl;
return 0;
}
If instead I wanted a function (or a single process to call in main) to return a 1-D array (int toBeReturned[size to be determined]
) I had to use a function that returns a pointer and then include that function in a macro that constructs the array.
Is there a simpler way of doing this in c++?
If not can someone please explain why this only works for type string? I thought that a string is simply a 1-D array of type 'char'.
Thank you,
Daniel