gpt4 book ai didi

c++ - 为什么我打印的是 Hp 而不是空行?

转载 作者:行者123 更新时间:2023-11-27 22:35:06 25 4
gpt4 key购买 nike

我有一个很简单的问题,为什么这段代码的输出是这样的?

我正在使用 Dev-C++ 5.11 和 TDM-GCC 4.9.2 64 位

#include <iostream>
using namespace std;
int main()
{
char *ptr;
char Str[] = "abcdefg";
ptr = Str;
ptr += 8;
cout << ptr;
return 0;
}

我希望代码打印一个空行。

由于某些原因,第 7 位似乎有一个空格字符,您可以通过将 ptr +=8; 更改为 ptr+=7; 来检测。

但对我来说更奇怪的是还有 3 个字符不能显示,除非你跳出数组限制 2,在本例中我们将 8 添加到指针。字符是:“H,(一个奇怪的填充方 block ),p”

screenshot of the output from my computer

最佳答案

I would expect the code to print an empty line.

这种期望是错误的。程序的行为未定义。

For some reason, there seems to be a space character at position 7

没有。第 7 位有一个空终止符。

but what is weirder to me is that there are 3 more characters that can't be displayed unless you jump beyond the array limit by 2 ...

在边界之外访问数组的行为是未定义的。

关于c++ - 为什么我打印的是 Hp 而不是空行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com