我无法理解 C++ 中的 char[] 行为
I can't understand char[] behavior in C++
向 Stack Overflow 社区致以问候。
我正在尝试从最基础的地方学习 C++,但遇到了一些奇怪的(至少对我而言)行为。这是我的 "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;
}
真正让我烦恼的是它给出的输出。
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!
我真的不明白那里发生了什么。更重要的是,如果我添加 "for" 构造,例如
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;
输出变化更奇怪
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!
可能是我太笨了或者什么的,但如果我连最基本的东西都做不好,我就无法在学习上取得进步。
很抱歉,如果相同的问题在某个时候已经得到回答,我会尽力搜索但没有成功。
所以你会很高兴地指出我在哪里是愚蠢的吗?
谢谢
P.S。我在 Win7x64m 下使用 NetBeans 8 和 Cygwin。
这是有问题的:
char Thing[]="";
您正在创建一个空的单字符数组,并将用户输入写入其中。
你第一次偶然得到了正确的输出,但后来数据被覆盖了,你得到了这个奇怪的输出。
创建具有足够缓冲区长度的字符串
char thing[256];
或者使用字符串对象。
变量Thing
是一个只有单个字符的数组。向其中写入多个字符将越界并导致 未定义的行为。
空字符串文字 ""
是单个字符数组,字符串终止符,编译器将使用它来初始化数组 Thing
为精确副本。
你基本上有两个解决方案,要么声明数组具有更大的大小:
char Thing[128] = "";
或者你使用the standard C++ string class:
std::string Thing;
我绝对推荐后一种解决方案。
正如其他答案所建议的那样,您使用 char
数组而没有真正初始化它(提供固定大小)。此外,这是 C 风格的编程。我强烈建议使用 C++ 的 std::string
而不是 char
-Array,因为这不会那么容易出错。
自代码编译以来,本地声明的此类数组的大小应该是固定的。
char Thing[] = "";
这给你一种错觉,认为'Thing'的大小是可变的。遗憾的是,'Thing' 的大小将根据右侧给出的初始值自动推导,这使得您输入的字符串写入内存中的意外位置,导致未定义的行为。
向 Stack Overflow 社区致以问候。 我正在尝试从最基础的地方学习 C++,但遇到了一些奇怪的(至少对我而言)行为。这是我的 "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;
}
真正让我烦恼的是它给出的输出。
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!
我真的不明白那里发生了什么。更重要的是,如果我添加 "for" 构造,例如
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;
输出变化更奇怪
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!
可能是我太笨了或者什么的,但如果我连最基本的东西都做不好,我就无法在学习上取得进步。 很抱歉,如果相同的问题在某个时候已经得到回答,我会尽力搜索但没有成功。 所以你会很高兴地指出我在哪里是愚蠢的吗? 谢谢
P.S。我在 Win7x64m 下使用 NetBeans 8 和 Cygwin。
这是有问题的:
char Thing[]="";
您正在创建一个空的单字符数组,并将用户输入写入其中。
你第一次偶然得到了正确的输出,但后来数据被覆盖了,你得到了这个奇怪的输出。
创建具有足够缓冲区长度的字符串
char thing[256];
或者使用字符串对象。
变量Thing
是一个只有单个字符的数组。向其中写入多个字符将越界并导致 未定义的行为。
空字符串文字 ""
是单个字符数组,字符串终止符,编译器将使用它来初始化数组 Thing
为精确副本。
你基本上有两个解决方案,要么声明数组具有更大的大小:
char Thing[128] = "";
或者你使用the standard C++ string class:
std::string Thing;
我绝对推荐后一种解决方案。
正如其他答案所建议的那样,您使用 char
数组而没有真正初始化它(提供固定大小)。此外,这是 C 风格的编程。我强烈建议使用 C++ 的 std::string
而不是 char
-Array,因为这不会那么容易出错。
自代码编译以来,本地声明的此类数组的大小应该是固定的。
char Thing[] = "";
这给你一种错觉,认为'Thing'的大小是可变的。遗憾的是,'Thing' 的大小将根据右侧给出的初始值自动推导,这使得您输入的字符串写入内存中的意外位置,导致未定义的行为。