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 ...
随机推荐
- Java enum枚举类型
java的枚举类型详解: 简单示例: public enum Color{ RED,BLUE,BLACK,YELLOW,GREEN } 复杂示例(带自定义构造方法与类型) public enum En ...
- [翻译]pytest测试框架(一)
此文已由作者吴琪惠授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 纯官网译文而已... pytest是一个成熟的.全功能的python测试工具. pytest框架编写测试用例 ...
- WPF 内部Template 动画板 无法冻结此 Storyboard 时间线树供跨线程使用
解决此问题,需要一定的想象力. 换个思路即可 大体是 使用Tag或者别无用的可以输入数值的属性,或者附加属性也可以的.来绑定到你要动画的属性 当然这个过程中要使用转换器了 我给出一个关于Button ...
- clickonce联机模式
发布时选择该程序只能联机使用,这样本地就不会进行安装. 参考地址:https://blog.csdn.net/dqs78833488/article/details/52513948
- C# 键盘中的按键对应KeyValue
首先先看一下什麼情況下需要對按鍵進行識別: KeyPress事件響應函數中,有KeyPressEventArgs, 對應於e.KeyChar; KeyDown事件響應中有KeyEventArgs 求取 ...
- BZOJ3786: 星系探索(伪ETT)
题面 传送门 题解 坑啊--我好像把\(Splay\)的东西全忘光了-- \(ETT\)(\(Euler\ Tour\ Tree\))是一种可以资瓷比\(LCT\)更多功能的数据结构,然而不管是功能还 ...
- BZOJ3210: 花神的浇花集会(坐标系变换)
题面 传送门 题解 坐标系变换把切比雪夫距离转化为曼哈顿距离 那么对于所有的\(x\)坐标中,肯定是中位数最优了,\(y\)坐标同理 然而有可能这个新的点不合法,也就是说不存在\((x+y,x-y)\ ...
- rpm -ivh 这个ivh是干什么的
安装的时候显示安装进度 --从百度知道复制过来的 RMP 是 LINUX 下的一种软件的可执行程序,你只要安装它就可以了.这种软件安装包通常是一个RPM包(Redhat Linux Packet Ma ...
- jquery源码解析:val方法和valHooks对象详解
这一课,我们将讲解val方法,以及对value属性的兼容性处理,jQuery中通过valHooks对象来处理. 首先,我们先来看下val方法的使用: $("#input1").va ...
- Ubuntu下实现socks代理转http代理
代理(英语:Proxy),也称网络代理,是一种特殊的网络服务,允许一个网络终端(一般为客户端)通过这个服务与另一个网络终端(一般为服务器)进行非直接的连接.一些网关.路由器等网络设备具备网络代理功能. ...