📅  最后修改于: 2020-09-25 09:04:13             🧑  作者: Mango
int memcmp( const void* lhs, const void* rhs, size_t count );
memcmp()
函数采用三个参数: lhs
, rhs
和count
。此函数首先将lhs
和rhs
指向的对象解释为unsigned char
数组。然后按字典顺序比较lhs
和rhs
的第一个count
字符 。
它在
memcmp() 函数返回:
#include
#include
using namespace std;
void display(char *lhs, char *rhs, int result, int count)
{
if(result > 0)
cout << rhs << " precedes " << lhs << endl;
else if (result < 0)
cout << lhs << " precedes " << rhs << endl;
else
cout << "First " << count << " characters of " << lhs << " and " << rhs << " are same" << endl;
}
int main()
{
char lhs[] = "Hello World!";
char rhs[] = "Hello Earth!";
int result;
result = memcmp(lhs, rhs, 5);
display(lhs, rhs, result, 5);
result = memcmp(lhs, rhs, 7);
display(lhs, rhs, result, 7);
return 0;
}
运行该程序时,输出为:
First 5 characters of Hello World! and Hello Earth! are same
Hello Earth! precedes Hello World!