user186840
user186840

Reputation:

C++ char* in methods

Listen people, i want to write a method that gets a line from the user, so i wrote this:

static char* getline(){
                char temp[0];
                cin >> temp;
                return temp;
         }

I also have a writeline method:

static void writeline(char* text){
 cout<<text<<endl;
 }

and then i go to the main and wrote this:

writeline(getline());

and it didnt work.. i want to say that when i wrote this:

static char* getline(){
                char temp[0];
                cin >> temp;
                writeline(getline());
                return temp;
         }

and i wrote in main this:

getline();

it did work!

so what do i need to do?

Upvotes: 0

Views: 1308

Answers (5)

Michael Ekstrand
Michael Ekstrand

Reputation: 29120

There are a few things going on here.

First, you have to allocate memory when working with char*; a zero-length array won't be long enough. It's a lot easier to just use std::string which takes care of all the dirty work for you. Further, an array declared with a size in your function is allocated on the stack rather than the heap; once your getline function returns, the memory is no longer valid.

Second, the >> operator for strings reads the next word, not the next line. For that you need the getline method. Here comes the tricky thing: getline doesn't use std::string, so you still need the char*. You just don't want to return one, because then you need to deallocate it unless it's a global (or static) array. So, what you can do is the following:

std::string getline() {
    char buf[1024];          // we have a cap on the line size
    cin.getline(buf, 1024);  // reads a line, up to 1023 characters
    return std::string(buf); // makes a copy of buf into a properly-managed string
}

void writeline(const std::string &s) {
    cout <<s <<endl;
}

Now you can do

writeline(readline());

and it should work fine. You can see here for more info on cin.getline().

Upvotes: 3

Gareth Simpson
Gareth Simpson

Reputation: 37701

There are a few things that are a bit suspect here.

First of all inside the getline() function you are declaring

char temp[0];

Which is an array of characters with no members. That can't possibly be what you intend.

Secondly your declaration of temp is as a variable on the stack. You can't use it as the return value of the function as it will go out of scope when you return.

Finally cin >> will only return a single character. Since your function is called getline I assume you want whole lines.

cin can do that for you using the getline function, but you need to pass it a buffer to put the line into.

See this reference: http://www.cplusplus.com/reference/iostream/istream/getline/

Upvotes: 0

Andrew Keith
Andrew Keith

Reputation: 7563

your returning corrupted memory, thats why

static char* getline(){
                char temp[0];
                cin >> temp;
                return temp;
         }

change this to

char temp[10000]; // put this outside in a class or global, give it some space
static char* getline(){
                cin >> temp;
                return temp;
         }

Upvotes: 0

Michael
Michael

Reputation: 55445

There are two issues with your code.

First, you are trying to read a variable size input into a fixed-size array that is not a sufficient size - temp[0] may only be a single byte long. You are overruning the array and stomping on other data that is on the stack (like the return address.) This could cause a crash or odd behavior when the function returns.

Second, you are returning a local variable from a function. As soon as the function returns, the variable is out of scope and its stack space will be reused. You need to actually allocate memory if you are going to return it from a function.

The reason the second version works is that the stack-based array is still valid during the call to getline(), and since arrays grow downward getline() is immune to any stack corruption that was caused.

Upvotes: 2

John Boker
John Boker

Reputation: 83729

I think the problem is that your char temp[0] is defined inside the function (local to the function). Another problem i see is the temp[0], how many characters are you trying to allocate for the text input?

Upvotes: 1

Related Questions