问个memcmp使用的问题
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int a=1;
int b=256;
if(memcmp(&a,&b,sizeof(int))<0)
cout<<"1<256"<<endl;
else
cout<<"1>256"<<endl;
return 0;
}
为什么输出 1>256,好像只比较了一个字节。
一直没搞清楚。
求教一下上述memcmp使用有什么问题?
多谢~
[解决办法]
a=1;在内存是这样存储的:01 00 00 00;
b=256;是这样的:00 01 00 00;
以上四个字节存储,该函数是按字节比较的,第一个字节,大小就已经确定,就不会往后比较,所以是大于0