当前位置:网站首页>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、代码测试
边栏推荐
- C language: quick sorting of sequential storage structure
- DXF读写:对齐尺寸标注文字居中、上方的位置计算
- Today's sleep quality record 75 points
- PHP generates random numbers (nickname random generator)
- 比XShell更好用、更现代的终端工具!
- 不用Swagger,那我用啥?
- Humiliation, resistance, reversal, 30 years, China should win Microsoft once
- 使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
- Is azvudine, a domestic oral new coronal drug, safe? Expert authority interpretation
- C语言:优化后的归并排序
猜你喜欢

用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历

DXF读写:对齐尺寸标注文字居中、上方的位置计算

不用Swagger,那我用啥?

比XShell更好用、更现代的终端工具!

You have to apologize if you get involved in the funny shop?

SQL daily practice (Niuke new question bank) - day 4: advanced operators

性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...

Volcanic stone investment Zhang Suyang: hard technology, the relatively certain answer in the next 10 years

拒绝服务 DDoS 攻击

Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
随机推荐
C语言:优化后的归并排序
Can second uncle cure young people's spiritual internal friction?
Understanding of stack and practical application scenarios
Uva1599 ideal path problem solution
PHP generates random numbers (nickname random generator)
[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
了解虚拟列表背后原理,轻松实现虚拟列表
111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误
Today's sleep quality record 75 points
Poj3268 shortest path solution
安全保障基于软件全生命周期-NetworkPolicy应用
R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize violin diagrams, set the palette parameter, and customize the border colors of violin diagrams at different l
SQL daily practice (Niuke new question bank) - day 4: advanced operators
面经整理,助力秋招,祝你称为offer收割机
Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
Rust from introduction to mastery 01 introduction
DOJNOIP201708奶酪题解
R language uses LM function to build linear regression model and subset function to specify subset of data set to build regression model (use floor function and length function to select the former pa
C language: merge sort
比XShell更好用、更现代的终端工具!