使用<iostream>打印指针

abe*_*eln 6 c++ iostream

为什么

#include <iostream>
using namespace std;

int main() {
  cout << (char*)0x10 << endl; 
}
Run Code Online (Sandbox Code Playgroud)

但是,段错误

#include <iostream>
using namespace std;

int main() {
  cout << (void*)0x10 << endl; 
}
Run Code Online (Sandbox Code Playgroud)

似乎工作得很好?

Luc*_*ore 8

因为

cout::operator <<(void*) 
Run Code Online (Sandbox Code Playgroud)

打印内存地址,和

cout::operator <<(char*)
Run Code Online (Sandbox Code Playgroud)

打印以null结尾的字符数组,并在尝试从中读取char数组时遇到未定义的行为0x10.