I am trying to return a char array in C++ using a getter function. This is academic and we are required to use char[]/char*, and then return it so we can cout
the array to the console.
char* Groceries:: getList() const
// Returns the list member.
{
return list;
}
I have also trying using strcpy
in that function. Both methods return what seems to be a memory location when I use cout
which resembles something like: "ám♥
". If i try to perform: cout << glistone.getList()[0];
it just returns the first character in the the previous line: "ám♥
".
I have read many, many SO questions on C Strings, etc., but none of the accepted solutions seem to do anything when I build/run my program other than return what I'm already returning.
How can you return a char* in C++, print it out, and not return the memory address?
`. What is "the same way"? You can use `std::cout` to print out `std::string`s. You can't return an `std::string` from a function returning `char *`. – The Paramagnetic Croissant Nov 09 '14 at 17:40