当给定一个数组时,cout 如何工作?

发布于 2024-10-24 18:25:22 字数 661 浏览 1 评论 0原文

可能的重复:
为什么 cout 打印 char 数组与其他数组不同?< /a>

如果我有这个代码:

char myArray[] = { 'a', 'b', 'c' };
cout << myArray;

它给我这个输出:

abc

但是,如果我有这个代码:

int myArray[] = { 1, 2, 3 };
cout << myArray;

它给我这个输出:

0x28ff30

为什么它不打印出123?

Possible Duplicate:
Why does cout print char arrays differently from other arrays?

If I have this code:

char myArray[] = { 'a', 'b', 'c' };
cout << myArray;

It gives me this output:

abc

However, if I have this code:

int myArray[] = { 1, 2, 3 };
cout << myArray;

It gives me this output:

0x28ff30

Why does it not print out 123?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

所有深爱都是秘密 2024-10-31 18:25:22

第一段代码起作用的原因是编译器将数组隐式转换为 const char * 字符指针,然后将其解释为 C 样式字符串。有趣的是,此代码安全,因为您的字符数组没有显式以 null 终止。因此,打印它将开始读取和打印字符,直到您碰巧找到一个空字节,这会导致未定义的行为。

在第二种情况下,编译器获取 int 数组并将其隐式转换为指向第一个元素的 int * 指针,然后从那里转换为 const void * 指向第一个元素的指针。使用 cout 打印 const void * 指针只会打印它的地址,从而得到您得到的输出。

希望这有帮助!

The reason that the first piece of code works is that the compiler is implicitly converting the array into a const char * character pointer, which it's then interpreting as a C-style string. Interestingly, this code is not safe because your array of characters is not explicitly null-terminated. Printing it will thus start reading and printing characters until you coincidentally find a null byte, which results in undefined behavior.

In the second case, the compiler is taking the int array and implicitly converting it into an int * pointer to the first element, then from there to a const void * pointer to the first element. Printing a const void * pointer with cout just prints its address, hence the output you're getting.

Hope this helps!

記柔刀 2024-10-31 18:25:22

有一个 运算符 << 知道左侧的 basic_ostream 实例(例如 cout),并且 const char* 位于右侧。

没有为 const int*(或 const int[])定义这样的运算符。尽管您完全可以自由地创建一个。

请务必在数组末尾指定一个哨兵,以防止超出缓冲区末尾。

您看到指针值的原因是因为有一个 basic_ostream::operator<<(const void*) 会打印它。

There is an operator << that knows about basic_ostream instances (such as cout) on the left-hand-side and const char*s on the right.

There is no such operator defined for const int* (or const int[]). Although you are perfectly at liberty to create one.

Just be sure to specify a sentinel at the end of your arrays to prevent running off the end of your buffer.

The reason you see the pointer value is because there is an basic_ostream::operator<<(const void*) which will print this.

薄荷梦 2024-10-31 18:25:22

std::cout 是 std::ostream 的实例,并且提供了几个重载运算符。

例如:

std::ostream& operator << (std::ostream&, char*);

当您输入 std::cout <<索梅瓦;编译器查找最匹配的重载。首先是变量的确切类型,然后是它可以隐式转换为的任何类型(更不用说成员函数/自由函数/模板函数等)。

这是一篇关于C++ 重载解决方案的随机文章

std::cout is an instance of std::ostream, and there are several overloaded operators provided.

For example:

std::ostream& operator << (std::ostream&, char*);

When you type std::cout << somevar; compiler looks up best matching overload. First for exact type of the variable, then for anything it can be implicitly converted to (not to mention member functions/free functions/template functions, etc).

Here is a random article on C++ Overload Resolution

無心 2024-10-31 18:25:22

当您在上下文中使用 myArray 时,cout << myArray;,它衰减为一个指针。将 char* 作为第二个参数的运算符<<输出一个字符串;接受其他类型指针的指针只输出一个地址。因此观察到的行为。

您的 char 数组实际上不是以 null 结尾的,所以我猜您在第一种情况下看到的实际上只是未定义的行为,在这种情况下恰好做了“正确的事情”。

When you use myArray in the context cout << myArray;, it decays to a pointer. The operator<< which takes a char* as its second argument outputs a string; the one which takes other types of pointer just outputs an address. Hence the observed behaviour.

Your char array is actually not null-terminated, so I guess what you're seeing in the first case is really just undefined behaviour which happens to do 'the right thing' in this instance.

╭⌒浅淡时光〆 2024-10-31 18:25:22

你还没有向它传递一个整数数组;你已经向它传递了一个指向 int 的指针。当遇到指针时,它会打印出它指向的地址。它无法打印出数组,因为它不知道它有多少个元素(如果有)。

当您使用指向字符的指针时它起作用的原因是它知道所有字符数组都以 NUL (\0) 字符终止,因此您没有这样做并不重要告诉它数组中的字符数。请记住,您的数组不是以 NUL 终止的,因此您只有运气好才能得到 abc 并且末尾没有多余的垃圾字符。

You haven't passed it an array of ints; you've passed it a pointer to an int. When faced with a pointer, it prints out the address that it points to. It has no way of printing out an array because it doesn't know how many elements it has (if any).

The reason it worked when you used a pointer to a character is that it knows that all arrays of characters are terminated by a NUL (\0) character, so it doesn't matter that you haven't told it the number of characters in your array. Keep in mind that your array is not terminated by a NUL, so it's only by luck that you got abc and no extra garbage characters on the end.

又怨 2024-10-31 18:25:22

因为它无法知道你的数组是数组,或者里面有什么样的数据。当你执行cout <<时myArray,'myArray'被视为指针类型,它可以指向任何东西。因此,不要尝试取消引用指针(如果指针尚未初始化,则可能会导致应用程序崩溃),而是打印指针指向的地址。

Because it has no way of knowing that your array is an array, or what kind of data is in it. When you do cout << myArray, 'myArray' is treated as a pointer type, which may point to anything. So instead of trying to dereference the pointer (and potentially crashing the app if the pointer has not been initialized), the address that the pointer is pointing to gets printed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文