Question:
return char array from function c++?
shanakaplus
2011-11-03 12:40:09 UTC
what is the correct way of declaring a function that return an array of characters? I want Write a programme that reads in the name (Give name + space + family name) .of 5 persons help me to get it work...

#include
#include
#include

using namespace std;
char read_file ();

void main(void)
{
char name [50] ;



for(int i=1;i<=5;i++)
{
name[50] = read_file ();
cout< }

}


char read_file () // read from the cmd
{
char line[50] ;

cout<<"enter your full name : ";
cin>> line;
return (line);

}
Five answers:
MichaelInScarborough
2011-11-03 14:17:10 UTC
As you were asking explicitly for an char array this is my answer.

Naturally the string type is far easier to handle and more powerful.

However, how to refer to returning a string type to be "the correct"

way is not really conclusive ... Again: you asked for char array and

the correct way to return a char array is shown here :O)



#include

using namespace std;



char *read_file();



int main(void)

{

char *name ;

for(int i=1;i<=5;i++){

name = read_file ();

cout<
delete [] name;

}

return 0;

}





char *read_file () // read from the cmd

{

char *line = new char[50] ;

cout<<"enter your full name : ";

cin.getline(line, sizeof(char) *50);

return (line);

}



Check this out: https://ideone.com/yJyoo
Linda
2016-05-16 09:30:01 UTC
The reason your getting an error on the return type is because you can not return an array in c++. Can return a pointer to array, but not the array itself. Also, it because you have the square brackets in the return statment. char* Needed::CopyText(char source[], char destination[], int start, int end) { for (int i=start; i <= end; i++) { destination[i] = source[i]; } return destination; } But for this is, you need to make sure you the variable you are return to is a pointer. Don't worry, it can still be an array. char x[20] = {"Hi Guyy"}; char y[20]; y = Copy(x,y,0,20); Thats will work like a charm (I tested it). You can even change the funtion up some and pass the arrays by reference so you do not even have to return the array. All the changes will saved. void Copy(char source[], char *destination, int start, int end) { for (int i=start; i <= end; i++) destination[i] = source[i]; } char x[20] = {"Hi Guys"}; char y[20]; This is the way I would do this.
Cubbi
2011-11-03 13:20:16 UTC
The correct way is to return a string, you've even #include'd the header file that makes it possible:



#include

#include



using namespace std;

string read_file ();

int main()

{

     for(int i=1;i<=5;i++)

     {

         string name = read_file();

         cout<
     }

}



string read_file () // read from the cmd

{

     string line;

     cout<<"enter your full name : ";

     getline(cin, line);

     return line;

}



test: https://ideone.com/E4B34



If you do in fact need to return an array, you have to allocate it dynamically and return an appropriate pointer, which is a lot more work: https://ideone.com/XLWcc
jplatt39
2011-11-03 13:19:03 UTC
No what you would probably do would be something like:



char *read_file()

{

char line[50] ;



cout<<"enter your full name : ";

cin>> line;

return (line);



}



Then in main()



char *name;



for (int i-1;i<=5;i+=1)

(

name=read_file();

cout << name << endl;

}



Yes that is a pointer to a character but the function allocates memory with char line[50] and even though it goes out of scope, so you might have a memory leak you would still have a pointer to a valid asciiz string when you assign name the address it returns.
oops
2011-11-03 14:20:20 UTC
Cubbi already answered your question perfectly. But something else should be pointed out to you, because you might try jplatt39's solution, and it might work for you, and you might then conclude that his solution is as good as Cubbi's.



It's not. If it happens to work for you, it's purely by accident. He is returning a pointer to a local array, then trying to read from it. It's not a memory leak, it's an access violation. This is undefined behavior. That means that even if it works for you today, that doesn't mean it will work on a different compiler, or even the same compiler with different settings.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...