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. postgres数据库参数配置说明介绍

    访问 1. listen_addresses 监听访问地址 2. port 监听端口 3. max_connections 最大连接数 4. 性能 1. shared_buffers PostgreS ...

  2. 调用kylin的restAPI接口构建cube

    调用kylin的restAPI接口构建cube 参考:http://kylin.apache.org/docs/howto/howto_build_cube_with_restapi.html 1. ...

  3. driver.get()和driver.navigate().to()到底有什么不同?-----Selenium快速入门(四)

    大家都知道,这两个方法都是跳转到指定的url地址,那么这两个方法有什么不同呢?遇到这种情况,第一反应就是查查官方的文档. 官方文档的说法是:Load a new web page in the cur ...

  4. css+html+JQuery 万能弹出层,居中显示

    function ShowMsg(str) {//要提示的文字 $(".payment_time_mask").remove(); $("body").appe ...

  5. LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

    http://lightoj.com/volume_showproblem.php?problem=1213  Fantasy of a Summation Time Limit:2000MS     ...

  6. ANE-调用原生组件横屏定位问题

    当我们的应用是横的时候,利用ANE调用原生组件如果处理不当,掉出来的组件会是竖的.那么我么要怎么做才能免去自己手动旋转组件这个破事呢.其实很简单 webView = [[UIWebView alloc ...

  7. C博客作业05—指针

    1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1 学习体会 理解了指针在代码中的使用方法,学会使用指针进行参数操作 学会了结构体的定义方式与结构体的使用 经过持续一个周 ...

  8. unix网络编程卷2:进程间通信

    管道没有名字,只能有亲缘关系使用. FIFO也叫有名管道,有名所以没有了这个限制. 管道提供一个单向数据流,创建函数返回两个文件描述符.一个用来读,一个用来写. 宏S_ISFIFO可用于确定一个描述符 ...

  9. openvswitch dpdk

    作者:张华  发表于:2016-04-07版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 硬件要 ...

  10. WPF InkCanvas EditingMode为Select时 在其选择时各种事件中撤销Select模式的方法

    InkCanvas有多种输入模式. 通过InkCanvasEditingMode来进行对其调整 分别是 None=0// 忽略鼠标和手写笔输入 Ink = 1// 允许用户绘制批注,默认模式.使用鼠标 ...