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 ...
随机推荐
- 如何查看非自己提交的请求的结果 - 深入浅出Oracle EBS之杂项技术荟萃
如何查看非自己提交的请求的结果定位要找的请求SQL举例:SELECT req.request_id, fcp.user_concurrent_program_name, usr ...
- 转-SourceTree注册atlassian账号SIGUP按钮灰色无法注册的问题
我们第一次安装sourcetree的时候会要求我们登陆一个账号 但是,会出现注册按钮变灰的怪现象 令人头疼的是,在官网也搞不定. 下面的网址可以顺利注册账号的网址 https://www.atlass ...
- ISO in CSS content
Name Numeric Description Hex ISO in CSS content Octal no-break space %A0 p:before { content: ...
- 基于微软XAML技术的前端开发方法
使用XAML技术的平台目前包括WPF,Silverlight,Windows8等平台,未来的Windows10统一Windows App也使用XAML技术. 前端开发指通过可视化集成开发环境进行用户界 ...
- CDI Event解析
CDI(Contexts And Dependency Injection)是JavaEE 6标准中一个规范,将依赖注入IOC/DI上升到容器级别, 它提供了Java EE平台上服务注入的组件管理核心 ...
- 杭州.Net 相关大公司,希望对大家有帮助
本人目前大四,还在实习.北京工作辞职后,打算回杭州看看.发现杭州的大公司相对北京好少啊,招.Net相关的公司就更少了... (我认为刚毕业生还是去大公司比较靠谱,一方面也是实力的体现)大学生,而且之前 ...
- JS判断时特殊值与boolean类型的转换
扒开JQuery以及其他一些JS框架源码,常常能看到下面这样的判断,写惯了C#高级语言语法的我,一直以来没能系统的理解透这段代码. var test; //do something... if(tes ...
- djang系列5.5-- 图书管理系统实例
一.表格设计 E-R图 分析图 models.py from django.db import models # Create your models here. class Author(model ...
- J - Judge(快速幂)(同余定理)
J - Judge Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit S ...
- JAVA判断质数
好久没写了,今天做题有点忘了,不会写了.重新做了一份,整理出来. import java.util.Scanner; public class 判断质数 { public static boolean ...