C++: Using and returning character arrays from functions, return type or reference?
I’m trying to create a null terminated string outside of a function, then run a function which will assign some data to it. For example, char abc [80] is created in main. input() is then run, which will return user input to abc. I figure the two obvious ways to do this are:
1.
Make the input function return the input to a variable in main, something like:
char input ()
{
char input [80];
getline(cin, choice);
return input;
}
int main ()
{
char choice [80];
choice = input ();
...
}
2.
Pass a character array to the input function my reference, and then put the data in it from there:
...
void input (&variable)
{
getline(*variable, cin);
return;
}
int main ()
{
char choice [80];
char* pointer;
input (pointer);
...
}
But, I can’t get either of these ways to work. So, what am I doing wrong and how can I fix it?
Just use standard strings. The insanity of using arrays of constant width has caused at least the company I work four many hundreds of thousands of dollars in development time. The correct answer to you question may be frustrating to you but it really is, “DON’T DO THAT!!!”