memcmp

  • Compare two blocks of memory.
  • Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.

    Notice that, unlike strcmp, the function does not stop comparing after finding a null character.
  • 把存储区 lhs 和存储区 的前 count 个字节进行比较。
  • 比较 lhs 和 rhs 所指向对象的首 count 个字节。比较按字典序进行。
  • 结果的符号是在被比较对象中相异的首对字节的值(都转译成 unsigned char )的差。
  • 若在 lhs 和 rhs 所指向的任一对象结尾后出现访问,则行为未定义。若 lhs 或 rhs 为空指针则行为未定义。
int memcmp( const void* lhs, const void* rhs, size_t count );

Parameters

lhs

  • Pointer to block of memory.
  • 指向内存块的指针。

rhs

  • Pointer to block of memory.
  • 指向内存块的指针。

count

  • Number of bytes to compare.
  • 要被比较的字节数。

Return Value

  • Returns an integral value indicating the relationship between the content of the memory blocks:

    • < 0 the first byte that does not match in both memory blocks has a lower value in lhs than in rhs (if evaluated as unsigned char values)
    • 如果返回值 < 0,则表示 lhs 小于 rhs
    • 0 the contents of both memory blocks are equal
    • 如果返回值 > 0,则表示 lhs 小于 rhs
    • > 0 the first byte that does not match in both memory blocks has a greater value in lhs than in rhs (if evaluated as unsigned char values)
    • 如果返回值 = 0,则表示 lhs 等于 rhs

Example

//
// Created by zhangrongxiang on 2018/2/8 16:57
// File memcmp
// #include <stdio.h>
#include <string.h> //C 库函数 int memcmp(const void *str1, const void *str2, size_t n)) 把存储区 str1 和存储区 str2 的前 n 个字节进行比较。
//比较按字典序进行。
typedef struct {
char *name;
} Stu; int main() {
char *str = "abc";
char *str2 = "abC";
int cmp = memcmp(str, str2, strlen(str) > strlen(str2) ? strlen(str) : strlen(str2));
printf("%d\n", cmp); // windows mingw -> 1 linux gcc 32 int i = 0;
int i2 = 10;
cmp = memcmp(&i, &i2, sizeof(int));
printf("%d\n", cmp);//windows mingw -> -1 linux gcc -10
printf("%d\n", (int) sizeof(short));//2 Stu stu = {.name = "zing"};
Stu stu2 = {.name = "zing"};
Stu stu3 = {.name = "Zing"};
Stu stu4 = {.name = "aing"}; /***********************************************/
printf("%d\n", (int) sizeof(Stu)); //8
printf("%d\n", (int) sizeof(stu.name)); //8
printf("%d\n", (int) strlen(stu.name)); //4
printf("%d\n", (int) sizeof(char *)); //8
printf("%d\n", (int) sizeof(int *)); //8
printf("%d\n", (int) sizeof(long *)); //8
/***********************************************/ cmp = memcmp(&stu, &stu2, sizeof(Stu));
printf("struct %d \n", cmp);//0
cmp = memcmp(&stu, &stu3, sizeof(Stu));
//printf("struct %d \n", cmp);//-5 具有不确定性 --> 内存对齐知识
cmp = memcmp(stu.name, stu3.name, sizeof(Stu));
printf("struct %d \n", cmp); // windows mingw -> -1 linux gcc 32
cmp = memcmp(stu.name, stu4.name, sizeof(Stu));
printf("struct %d \n", cmp); // windows mingw -> -1 linux gcc 25 cmp = memcmp(&"abC", &"abc", sizeof(char *));
printf("%d\n", cmp);//-32
printf("a -> %d;A -> %d \n", 'a', 'A');//a -> 97;A -> 65
printf("z -> %d;a -> %d \n", 'z', 'a');//z -> 122;a -> 97
return 0;
}

参考文章

转载注明出处

C 标准库 - string.h之memcmp使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  5. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  6. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  7. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

  8. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

  9. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

随机推荐

  1. [编译,报错以及其他] 有关C/C++中int不能用-2147483648当最小值的问题

    这个取决于今早看耗子叔的微博: 这里说到了int的取值范围的问题,int的取值是-2147483648 ~ 2147483647,但是如果直接在编译器(VS2013)中使用-2147483648会报错 ...

  2. 基于pscp批量分发文件

    用法: pscp -h 目标ip文件 本地文件路径 远程路径 pscp -h hosts.ip file.txt ~/

  3. 精妙SQL语句大全

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  4. leetcode 回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  5. Visifire图表

    引用DLL: WPFToolkit WPFVisifire.Charts.dll WPFVisifire.Gauges.dll 1.柱状图 代码: public void BindChart1() { ...

  6. Javascript 535种方式!!!实现页面重载

    原文地址: http://www.phpied.com/files/location-location/location-location.html 完全出于好玩,竟然有人整理了500多种方法来实现刷 ...

  7. COOKIE的优化与购物车小试

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 一 Cookie 的优化 1.1 一般而言,我们设置cookie是在ph ...

  8. 九、基础正则表达式BRE

    1.重要性:简单的说正则表达式就是处理一套字符串的规则和方法,以行为单位对字符串进行处理. 运维工作中,会有大量的访问日志,错误日志,大数据学习正则表达式是不可少的. 2.linux正则表达式,主要是 ...

  9. 北航操作系统实验2019:Lab4-1代码实现参考

    北航操作系统实验2019:Lab4-1代码实现参考 部分实现参考自Github前辈们的项目,经过一定程度的勘误. 如果这份代码中存在任何问题或错误,请务必不吝在评论区指出. Exercise 4.1 ...

  10. 【12c OCP】最新CUUG OCP-071考试题库(49题)

    49.(11-1) choose the best answer Examine the structure of the SHIPMENTS table: You want to generate ...