user1093534
user1093534

Reputation: 21

C++ char array gives strange output

I am trying to create a char array in a function, pass it to main, and the print it out. This is giving me a string of random characters like this: ╠╠╠╠╠╠╠╠↑

Also, when I check the number of characters before the '\0' terminator the first output will give me the number of characters inputted, but the second output will give me 49 (size of the buffer), so I feel this is a problem involving the NULL terminator, but Im not sure how to resolve it. .

I have recreated the issue outside of my real project file, so the code is less messy and shown below.

const int BUFFER = 50;

char* getWord()
{
        char word[BUFFER];

    cout << "Please enter a word: ";
    cin.getline(word, BUFFER);
        cout << "The word is: " << word; // This prints out the word with no problems..

    return word;
}

int main()
{
        char* wordPtr;

    wordPtr = getWord();
    cout << "Your word is: " << wordPtr << "\n";  // This prints out random symbols.
    system("PAUSE");

    return 0;
}

Any help is greatly appreciated.

Upvotes: 1

Views: 5763

Answers (4)

user1089692
user1089692

Reputation:

You can use static char word[BUFFER] and return the address of the variable.

Upvotes: 1

KoKuToru
KoKuToru

Reputation: 4115

You can't return a local array.

const int BUFFER = 50;

void getWord(char* word, int size)
{
    cout << "Please enter a word: ";
    cin.getline(word, size);
    cout << "The word is: " << word;
}

int main()
{
    char word[BUFFER];

    getWord(word, BUFFER);
    cout << "Your word is: " << word << "\n"; 
    system("PAUSE");

    return 0;
}

C++ version:

string getWord()
{
    string word;
    cout << "Please enter a word: ";
    getline(cin, word);
    cout << "The word is: " << word;

    return word;
}

int main()
{
    string word;

    word = getWord();
    cout << "Your word is: " << word << "\n";
    system("PAUSE");

    return 0;
}

Upvotes: 4

Alok Save
Alok Save

Reputation: 206508

You are returning a pointer to a array local to the function.
This array does not exist once the function returns and your program suffers from the dreaded Undefined Behavior.
This implies that once you refer to this array from outside the function, the array may or may not contain the proper values.You cannot rely on the array being valid.

Suggested Solution:
You should allocate the pointer dynamically on freestore inside the function or use the C++ way of doing it i.e: Use std::string and return it by value.

Remember, that in C++ always use std::string over raw caharacter arrays unless for some specific reason you are forced to use the latter(such situations are rare).

Upvotes: 3

hmjd
hmjd

Reputation: 121961

You cannot return a pointer or reference to a local variable as it will no longer exist once the function returns.

A possible fix:

std::string getWord()
{
    std::string word;

    cout << "Please enter a word: ";
    std::getline(cin, word);
    cout << "The word is: " << word; // This prints out the word with no problems..

    return word;
}

Upvotes: 2

Related Questions