当前位置:网站首页>strcmp、strstr、memcpy、memmove的实现
strcmp、strstr、memcpy、memmove的实现
2022-07-28 12:50:00 【王红花x】
一、strcmp
1、定义
int strcmp(const char* str1, const char* str2);2、功能解析
这个函数会比较两个字符串的第一个字符。如果它们相等,就继续比较下一对,直到字符不同或者遇到 '\0' 。str1和str2是两个待比较的字符串,如果第一个不匹配的字符在str1中的小,返回一个 <0 的值,如果在str1中的打,返回一个 >0 的值,两个字符串内容相等则返回 0 .
3、功能实现

4、代码测试

二、strstr
1、定义
const char* strstr(const char* str1, const char* str2);2、功能解析
这个函数会返回一个指针,指向 str2 第一次在 str1 中出现的位置,如果 str2 不是 str1 的一部分,就返回一个空指针。匹配过程不会包括 '\0' ,但是会在 '\0' 处停止。
3、功能实现
#include <stdio.h>
#include <assert.h>
const char* MyStrstr(const char* str1, const char* str2)
{
assert(str1 && str2);
//如果str1找到了末尾,就结束循环
while (*str1)
{ //找到第一对匹配的字符
while (*str1 != *str2 && *str1 != '\0')
str1++;
int i = 0;
//这时候出现了第一对相等的字符,继续往下匹配
while (*(str1 + i))
{
//因为第一对字符已经匹配过相等,所以i可以先++
i++;
//匹配过程中如果str2遇到了'\0',证明str1中有str2的内容
if (*(str2 + i) == '\0')
return str1;
//如果在'\0'之前匹配到不相等的,就进行下一次的匹配
if (*(str1 + i) != *(str2 + i))
break;
}
//如果str1 + i 是'\0',意味着str1中已经不可能包含str2了
if (*(str1 + i))
str1++;
else
return NULL;
}
if (*str2 == '\0')
return str1;
else
return NULL;
}
4、代码测试

三、memcpy
1、定义
void* memcpy(void* destination, const void* source, size_t num);2、功能解析
这个函数会将 num 个字节的值从 source 指向的位置拷贝到 destination 指向的内存块;source 指针和 destination 指针指向的对象的底层类型与函数本身无关,结果是数据的二进制拷贝;函数不会在 source 中检测任何终止空字符,它会准确地拷贝 num 个字节;为了避免越界,destination 和 source 参数指向的数组大小,应该至少是 num 个字节,并且不会重叠。(对于重叠的内存块,memmove 是一个更安全的函数。)
destination 被返回。
3、功能实现

4、代码测试

四、memmove
1、定义
void* memmove(void* destination, const void* source, size_t num);2、功能解析
在解析这个函数的功能之前,我们先来用上一个函数做一个事;
很明显,memcpy函数无法做到,原因很简单,当想将 ‘c’ 拷贝的时候,'c' 原本的内容以及被 'a' 给覆盖,所以无法实现拷贝。因此,对于重叠的内存块,memcpy显得不安全。
接下来我们看memmove函数,这个函数可以实现内存块的移动。从 sourc 指向的位置拷贝 num 个字节的值到 destination 指向的内存块。进行拷贝事似乎使用了一个中间缓冲器,允许 destination 和 source 重叠。
source 指针和 destination 指针指向的对象的底层类型与函数本身无关,结果是数据的二进制拷贝;函数不会在 source 中检测任何终止空字符,它会准确地拷贝 num 个字节;为了避免越界,destination 和 source 参数指向的数组大小,应该至少是 num 个字节;
destination 被返回。
3、功能实现
这个函数在实现时,其实很简单,只要开辟一块空间拷贝一份原数据就可以实现。但我们可以试着尽可能少地开辟空间。

当 source 指向的位置在 destination 的前面时,我们从最后一个数据 5 开始拷贝,可以看到在拷贝之前,待拷贝的数据不会被覆盖

而当 source 指向的位置在 destination 的后面时,从第一个数据 5 开始拷贝,也可以实现这样的效果。

4、代码测试
边栏推荐
- 算法---不同路径(Kotlin)
- Uva11175 digraph D and E from D to e and back
- 浅谈WebSocket
- Poj1860 currency exchange solution
- Humiliation, resistance, reversal, 30 years, China should win Microsoft once
- C language: quick sorting of sequential storage structure
- 国产API管理工具Eolink太好用了,打造高效的研发利器
- Blue Bridge Training (additional interview questions) day 7
- SQL每日一练(牛客新题库)——第4天:高级操作符
- Jar package
猜你喜欢

What is the reason why the words behind word disappear when typing? How to solve it?

How to play a data mining game entry Edition

Three men "running away" from high positions in the mobile phone factory

111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误

【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式

How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question

Socket类关于TCP字符流编程的理解学习

30 day question brushing training (I)

The domestic API management tool eolink is very easy to use, creating an efficient research and development tool

Cool operation preheating! Code to achieve small planet effect
随机推荐
Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
Better and more modern terminal tools than xshell!
30 day question brushing training (I)
30天刷题计划(二)
word打字时后面的字会消失是什么原因?如何解决?
DOJNOIP201708奶酪题解
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间(设置conf.level参数指定置信水平、置信区间的大小)
Jar package
What if the server cannot be connected (the original server cannot find the target resource)
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
Operator3-设计一个operator
Remember to use pdfbox once to parse PDF and obtain the key data of PDF
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
No swagger, what do I use?
R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用floor函数和length函数选择数据前部分构建回归模型)
Intra prediction and transform kernel selection based on Neural Network
Uva11175 digraph D and E from D to e and back