-2

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?

boom_box
  • 127
  • 1
  • 2
  • 9
  • 4
    You don't. You return an `std::string` instead. If you **must,** then you allocate an array using `new` and return a `unique_ptr` to the caller. – The Paramagnetic Croissant Nov 09 '14 at 17:35
  • [Related stuff!](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – πάντα ῥεῖ Nov 09 '14 at 17:37
  • What is the type of `list`? You really should show the definition of `Groceries`. Just a guess but you should probably be returning a `const char*` - `const char* Groceries::getList() const`. – Captain Obvlious Nov 09 '14 at 17:38
  • I suggest you use a debugger, place a breakpoint at the return statement and verify the value of `list`. – Thomas Matthews Nov 09 '14 at 17:39
  • @The Paramagnetic Croissant - If I return `std::string`, am I able to still print out indices of that string in the same way? Also, can you return `std::string` from a function that states it returns a char*? We are not allowed to change the return types of the functions. – boom_box Nov 09 '14 at 17:39
  • @boom_box use backticks instead of ``. 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
  • @The Paramagnetic Croissant - for instance, `list[1]`, once I convert it to an `std::string`, can I use [1] on the new string to achieve the same value as `list[1]`? – boom_box Nov 09 '14 at 17:43
  • 1
    @boom_box Yes, but this can be trivially answered by reading the documentation of `std::string`. Why don't you do that? – The Paramagnetic Croissant Nov 09 '14 at 17:45
  • Why is everyone so confrontational when answering these questions? It's overwhelming to try and pick up all this stuff in a short amount of time, let alone reading the entire documentation for a new language. In the class I'm in now, we were given a C project, having never been taught C in this curriculum and we have to learn the language ourselves while doing a project in 2 weeks. Once we were done with that, they give us a C++ project, again 2 weeks, and again no teaching of the language. Sorry if my curriculum doesn't allow me the time to read all documentation that exists... – boom_box Nov 09 '14 at 17:56
  • The simple answer to this is create a struct that contains a fixed buffer length. Your function can return that by value. Very easy. – Xofo Jan 04 '18 at 05:14

3 Answers3

1

Here is an example of a function that returns a C-Style string and the main function that prints it.

#include <stdio>

char * a_function(void)
{
  static const char my_test[] = "Propane explodes.\n";
  return &my_test[0]; // It's a MISRA thing.
}

int main(void)
{
  char * p_text = 0;
  p_text = a_function();
  std::cout << Here's the text: "
            << p_text;
  std::cout.flush(); // Just to be sure.
  return 0;
}

In your function, I recommend you use a debugger and verify the value of list.

The example above uses hard-coded char arrays, to prove the concept. You have a variable, which we don't know the type, nor do we know how it gets its value, so YOU will have to discover it's contents. Debuggers can help you with this.

Edit 1
For the purists, I know that the function should return a char const *, but I wanted to match the OP's function, but using constant data.

Here's another version of the function:

char * another_function(void)
{
  static char text_buffer[64];
  strcpy(text_buffer, "Use a debugger!\n");
  return text_buffer;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

If you are absolutely required to use a C-style array and pointers, then you need to use new, and preferably smart pointers:

std::unique_ptr<char[]> getList() const {
    const char *someCString = // whatever
    char *p = new char[strlen(someCString) + 1];
    strcpy(p, someCString);
    return std::unique_ptr<char[]>(p);
}

and in the caller:

auto p = object.getList();
std::cout << p.get() << "\n";

If you can't use smart pointers either, then delete[] the returned pointer in the caller when it's not used anymore, so as to free the memory.

0

The problem has nothing common with the function definition though its return type could be defined as const char *

The problem is that the string that is stored in or pointed to by list is not zero terminated and it seems contains some garbage.

So search the error where you are modifying list.

Also your question

How can you return a char* in C++, print it out, and not return the memory address?

is contradictory. If list is defined as an array you could return a reference to this array instead of the pointer if you do not want to return the pointer.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335