我设计了一个 C++ 代码来检查机器的字节序。它运作良好。但是,它不能以 4 字节 int 打印出每个字节的内容。
#include<iostream>
using namespace std;
bool f()
{
int a = 1;
char *p = (char*)&a;
for (int i = 0 ; i < 4 ; ++i)
cout << "p[" << i << "] is " << hex << *p++ << " ";
cout << endl ;
p -= 4;
if (*p == 1) return true ; // it is little endian
else return false; // it is big endian
}
int main()
{
cout << "it is little endian ? " << f() << endl ;
return 0 ;
}
输出:
p[0] is p[1] is p[2] is p[3] is
it is little endian ? 1
为什么输出为空?
谢谢