我正在试验 C++,我决定尝试 is_big_endian 代码,就像我在 C 中做的一样。但是,当我尝试打印出指针的值时,我没有得到任何输出。我尝试了 C 和 C++ 风格的转换。我做错了什么?
#include <iostream>
using namespace std;
int main (void){
int num = 1;
char *ptr = (char *)#
//char *ptr = reinterpret_cast<char *>(&num);
cout << "Value is: " << *ptr << endl;
}
最佳答案
operator<<
看到您正在输出 char
, 所以它将它打印为一个字符,而不是一个数字(就好像你在 C 中写了 %c
而不是 %d
中的 printf
);自 *ptr
将是 0 或 1,在这两种情况下你都会以不可打印的字符结束。
要解决此问题,请显式转换 *ptr
至 int
:
cout << "Value is: " << int(*ptr) << endl;
关于c++ - big_endian 检查的 c++ 程序中没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090342/