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. IPv4&&IPv6地址结构分析

    IPv4套接字地址结构: 套接字都需要有一个指向套接字地址结构的指针作为参数.每个协议簇都定义它自己的套接字地址结构.这些结构的名字均已sockaddr_开头,并以对应每个协议族的唯一后缀结尾. wi ...

  2. C#基础入门 十一

    C#基础入门 十一 复选框 复选框的应用--问卷调查 实现描述:通过问卷调查统计性别和下班后回家的方式,单击"提交"按钮,通过消息框显示所提交的信息.运行结果如下图所示(图27): ...

  3. zabbix监控cpu jumps

    cpu监控图形分为三种 cpu jumps cpu突发 包含 context switches per second 进程线程切换 interrupts per second 每秒的中断次数 cpu ...

  4. 获取web项目中的webroot目录路径

    备忘,一段代码: @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-genera ...

  5. Linq中的group by多表多字段

    在sql中,如果有group by,那么select的字段只能包含分组内容,或者count.sum.avg这些统计字段. 但在linq里面,是:group 你想要什么字段 by 分组字段 比如: va ...

  6. Visual Studio Code Angular4 配置环境

    首先在本机安装node.js才能用 npm 命令 环境搭建 安装node.jsnpm install -g @angular/cli 安装第三方库npm install --save bootstra ...

  7. SQL 从数据库中随机取n条数据

    用NEWID()方法. * ,NEWID() AS random from [toblename] order by random 其中的1可以换成其他任意整数,表示取的数据条数

  8. Devexpress Tab Control 文档

    https://documentation.devexpress.com/WPF/8078/Controls-and-Libraries/Layout-Management/Tab-Control/P ...

  9. ASP.net 居中

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  10. [php审计实战篇]BlueCms v1.6 Union注入

    非常基础的代码审计练习,适合有php基础的审计新手练习 本文作者:Aedoo 来源:i春秋社区 0×01 代码跟踪 首先,进入首页代码 :index.php 包含了php文件:/include/com ...