Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
#include<iostream> using namespace std; int main(){ char s[10] = "abcde"; char* first = s - 1; cout << first << endl; return 0; }
当我运行它时,我的控制台中出现空白,但是当我说 *first = s; 我将整个 char 数组打印到我的控制台。我的问题是,当我将它设置为 s - 1 时,首先指向的是什么?
当您将指针设置为 时,该指针指向内存中位于分配的字符串前s-1一位置的字符。char取消引用此类指针是未定义的行为 - 程序可能会打印任何内容,甚至崩溃。
s-1
char
first将指向一块未分配的内存 - 行为未定义
first