简体   繁体   English

我无法理解C ++中的char []行为

[英]I can't understand char[] behavior in C++

Greeting to the Stack Overflow community. 向堆栈溢出社区致意。 I'm trying to learn C++ from the very basics, and encountered some strange (to me at least) behavior. 我试图从最基础的方面学习C ++,并且遇到了一些奇怪的(至少对我来说)行为。 This is my "hello world" 这是我的“你好世界”

#include <iostream>

int main() 
{
    std::cout << "Enter Thing! " << std::endl;
    char Thing[]="";
    std::cin >> Thing;
    std::cout << "Thing is " << Thing << "!" << std::endl;
    int i=0;
    while (i <= 10)
    {
        ++i;
         std::cout << "Thing is " << Thing << " in " << i << " while!" << std::endl; 
         std::cout << "i is " << i << "!" << std::endl;
    }
    std::cout << "Thing is " << Thing << " after while!" << std::endl;
    std::cout << "i is " << i << ".5!" << std::endl;
}

What actually bugs me is the output it gives. 真正困扰我的是它提供的输出。

Enter Thing!
thing
Thing is thing!
Thing is t☺ in 1 while!
i is 1!
Thing is t☻ in 2 while!
i is 2!
Thing is t♥ in 3 while!
i is 3!
Thing is t♦ in 4 while!
i is 4!
Thing is t♣ in 5 while!
i is 5!
Thing is t♠ in 6 while!
i is 6!
Thing is t in 7 while!
i is 7!
Thing is  in 8 while!
i is 8!
Thing is t       in 9 while!
i is 9!
Thing is t
 in 10 while!
i is 10!
Thing is t♂ in 11 while!
i is 11!
Thing is t♂ after while!
i is 11.5!

I don't really get what happens there. 我真的不明白那里发生了什么。 Even more, if i add "for" construction, for example 甚至更多,例如,如果我添加“用于”构造

for (int i=4; i <=7; ++i)
    {
        std::cout << "i is " << i << "!" << std::endl;
        std::cout << "Thing is " << Thing << " for!" << std::endl;
    }
    std::cout << "i is " << i+3 << ".5!" << std::endl;    
    std::cout << "Thing is " << Thing << " after for!" << std::endl;

output changes to even more strange thing 输出更改为更奇怪的事情

Enter Thing!
thing
Thing is thing!
Thing is thing☺ in 1 while!
i is 1!
Thing is thing☻ in 2 while!
i is 2!
Thing is thing♥ in 3 while!
i is 3!
Thing is thing♦ in 4 while!
i is 4!
Thing is thing♣ in 5 while!
i is 5!
Thing is thing♠ in 6 while!
i is 6!
Thing is thing in 7 while!
i is 7!
Thing is thin in 8 while!
i is 8!
Thing is thing   in 9 while!
i is 9!
Thing is thing
 in 10 while!
i is 10!
Thing is thing♂ in 11 while!
i is 11!
Thing is thing♂ after while!
i is 11.5!
i is 4!
Thing is t♦ for!
i is 5!
Thing is t♣ for!
i is 6!
Thing is t♠ for!
i is 7!
Thing is t for!
i is 14.5!
Thing is  after for!

Probably it's me being stupid or something, but i can't progress on learning if i can't even make the most basic thing work. 可能是我愚蠢或某事,但是如果我什至不能使最基本的事情都起作用,我就无法继续学习。 And sorry if the same question was already answered at some point, did my best to search for it with no success. 抱歉,如果某个问题已经解决,请尽我最大的努力寻找答案,但没有成功。 So would you gladly point me to where am i being stupid? 所以,你会高兴地指出我在哪里吗? Thanks. 谢谢。

PS I'm under Win7x64m using NetBeans 8 with Cygwin. PS我在Win7x64m下使用NetBeans 8和Cygwin。

The variable Thing is an array of only a single character . Thing变量是仅包含一个字符的数组。 Writing more than one character to it will be writing out of bounds and lead to undefined behavior . 向其写入多个字符将超出范围并导致不确定的行为

The empty string literal "" is an array of a single character, the string terminator, and the compiler will use that to initialize the array Thing to be an exact copy. 空字符串文字""是一个包含单个字符的数组,即字符串终止符,编译器将使用该数组将Thing数组初始化为精确的副本。

You basically have two solutions, either you declare the array to have a larger size: 基本上,您有两种解决方案,要么声明数组的大小较大:

char Thing[128] = "";

Or you use the standard C++ string class : 或者您使用标准的C ++字符串类

std::string Thing;

I definitely recommend the latter solution. 我绝对推荐后一种解决方案。

This is problematic: 这是有问题的:

 char Thing[]="";

You're creating an empty array of single char, and write the user input into it. 您正在创建一个包含单个字符的空数组,并将用户输入写入其中。

You have the correct output by chance for the first time, but then the data is overwritten and you get this weird output. 第一次您偶然有正确的输出,但是随后数据被覆盖,您得到了这个奇怪的输出。

Create the string with sufficient buffer length 创建具有足够缓冲区长度的字符串

char thing[256];  

or use the string object. 或使用字符串对象。

As the others answers suggest, you use your char array without really initialising it (supplying a fixed size). 正如其他答案所建议的那样,您无需真正初始化就可以使用char数组(提供固定大小)。 Furthermore this is C-style programming. 此外,这是C风格的编程。 I highly suggest using C++'s std::string instead of a char -Array, as this won't be so error prone. 我强烈建议使用C ++的std::string而不是char -Array,因为这不会那么容易出错。

Size of such array declared locally should be fixed since the code is compiled. 由于代码已编译,因此在本地声明的此类数组的大小应固定。

char Thing[] = "";

This gives you an illusion that size of 'Thing' is variable. 这给您一种幻想,“事物”的大小是可变的。 Sadlly, size of 'Thing' will be automatically deduced according to the initial value given on the right, which makes the string you input written on an unexpected position in the memory, causing undefined behaviour. 遗憾的是,“物”的大小将根据右边给出的初始值自动推导出,这使您输入的字符串写在内存中的意外位置,从而导致不确定的行为。

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

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