C 标准库 - string.h之memcmp使用
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;
}
参考文章
- http://zh.cppreference.com/w/c/string/byte/memcmp
- http://www.cplusplus.com/reference/cstring/memcmp/
- http://www.runoob.com/cprogramming/c-function-memcmp.html
转载注明出处
C 标准库 - string.h之memcmp使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
- C 标准库 - string.h之strpbrk使用
strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...
- C 标准库 - string.h之strrchr使用
strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...
随机推荐
- delphi跨平台SOCKET--System.Net.Socket
delphi跨平台SOCKET--System.Net.Socket 不知始于DELPHI哪一个版本,姑且始于柏林版吧. 基于此单元的TSocket类,大家可以很方便地封装出自己的服务端和客户端的SO ...
- 博客和Github简单练习
我的第一篇博客 1.首先是自我介绍 姓名:孙弘毅 班级:网工142 学号:1413042050 兴趣:游戏,看书 至于我写了多少代码我也不清楚,反正不多 2.Github 注册流程 (1)百度Git ...
- CDH4.1.2 集群安装配置详细过程
http://wenku.baidu.com/link?url=Wu43MFbzKH8hu7AgGfajmOr0WpRMX_gJlMDUs6pSrBK2LOJWIMpfWZa7IW-BSPko1yGl ...
- 洛谷P4586 [FJOI2015]最小覆盖双圆问题(最小圆覆盖)
题面 传送门 前置芝士 最小圆覆盖 题解 我们按照\(x\)坐标排序,然后二分中间点,把点分成左右两边,对两边都做一个最小圆覆盖,那么半径大一点的那个就是答案了.然后对半径大的那一边继续二分就行了 然 ...
- logstash同步mongodb数据到elasticsearch
一.安装logstash 二.安装mongodb插件 cd D:\Software\ELK5.5.0\logstash-5.5.0\bin logstash-plugin install logsta ...
- yum 安装和卸载
yum list列出资源库中特定的可以Linux Yum安装或更新以及已经安装的rpm包 安装 yum install 安装包关键字 卸载 yum remove 安装包名
- Mysql6.0连接中的几个问题 Mysql6.xx
Mysql6.0连接中的几个问题 在最近做一些Javaweb整合时,因为我在maven官网查找的资源,使用的最新版,6.0.3,发现MySQL连接中的几个问题,总结如下: 1.Loading clas ...
- 1. Socket网络编程
1. 借助服务器实现小写转大写的程序: 客户端:发送任意小写字母到服务器端. 服务器端:接收小写字母,转为大写,回传给客户端,然后客户端显示到屏幕. #include <stdio.h> ...
- Dota2APP--第二天
一.今天的任务 1)自定义标签栏控制器 2)自定义导航栏控制器 3)在新特性界面播放音频 1.第一个任务:自定义标签栏控制器 原因:默认的TabbarViewController不能满足项目的需求. ...
- 使用HBuilderX实现打包vue项目成app
一.准备开发工具 开发工具:HBuilderX 官网地址:http://www.dcloud.io (标准版需要自己安装插件,app开发版已经把app开发常用的插件安装好了,开箱即用,建议使用开发版) ...