简体   繁体   English

C ++ char数组给出奇怪的输出

[英]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. 我试图在一个函数中创建一个char数组,将其传递给main,然后将其打印出来。 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. 另外,当我检查'\\ 0'终止符之前的字符数时,第一个输出将给我输入的字符数,但是第二个输出将给我49个字符(缓冲区大小),所以我觉得这是一个问题涉及NULL终止符,但我不确定如何解决。 .

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. 任何帮助是极大的赞赏。

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: C ++版本:

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;
}

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 ie: Use std::string and return it by value. 您应该在函数内部的freestore上动​​态分配指针,或使用C ++方式实现该指针,即:使用std::string并按值返回它。

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). 请记住,在C ++中,始终对原始caactacter数组使用std::string ,除非出于某些特定原因而被迫使用后者(这种情况很少见)。

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;
}

您可以使用static char word[BUFFER]并返回变量的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM