当前位置:网站首页>Implementation of StrCmp, strstr, memcpy, memmove
Implementation of StrCmp, strstr, memcpy, memmove
2022-07-28 13:57:00 【Wang Honghua x】
One 、strcmp
1、 Definition
int strcmp(const char* str1, const char* str2);2、 Function parsing
This function compares the first character of two strings . If they are equal , Just continue to compare the next pair , Until the characters are different or encounter '\0' .str1 and str2 Are two strings to be compared , If the first mismatched character is str1 Small in the middle , Return to one <0 Value , If in str1 Hit in , Return to one >0 Value , If the contents of two strings are equal, it returns 0 .
3、 Function realization

4、 Code testing

Two 、strstr
1、 Definition
const char* strstr(const char* str1, const char* str2);2、 Function parsing
This function will return a pointer , Point to str2 For the first time in str1 Where in , If str2 No str1 Part of , Just return a null pointer . The matching process will not include '\0' , But it will be there. '\0' Stop at .
3、 Function realization
#include <stdio.h>
#include <assert.h>
const char* MyStrstr(const char* str1, const char* str2)
{
assert(str1 && str2);
// If str1 End found , Just close the loop
while (*str1)
{ // Find the first pair of matching characters
while (*str1 != *str2 && *str1 != '\0')
str1++;
int i = 0;
// At this time, the first pair of equal characters appear , Continue to match
while (*(str1 + i))
{
// Because the first pair of characters have been matched equal , therefore i You can start with ++
i++;
// If str2 Yes '\0', prove str1 There is str2 The content of
if (*(str2 + i) == '\0')
return str1;
// If in '\0' Before matching to unequal , Just make the next match
if (*(str1 + i) != *(str2 + i))
break;
}
// If str1 + i yes '\0', signify str1 It is impossible to include str2 了
if (*(str1 + i))
str1++;
else
return NULL;
}
if (*str2 == '\0')
return str1;
else
return NULL;
}
4、 Code testing

3、 ... and 、memcpy
1、 Definition
void* memcpy(void* destination, const void* source, size_t num);2、 Function parsing
This function will num A value of bytes from source Copy the location pointed to destination Point to memory block ;source Pointers and destination The underlying type of the object pointed to by the pointer is independent of the function itself , The result is a binary copy of the data ; The function will not be in source Detect any terminating null characters in , It will accurately copy num Bytes ; To avoid crossing the border ,destination and source The size of the array pointed to by the parameter , It should be at least num Bytes , And will not overlap .( For overlapping blocks of memory ,memmove Is a safer function .)
destination Returned .
3、 Function realization

4、 Code testing

Four 、memmove
1、 Definition
void* memmove(void* destination, const void* source, size_t num);2、 Function parsing
Before parsing the function of this function , Let's do one thing with the last function ;
Obviously ,memcpy Function can't do , The reason is simple , When you want to ‘c’ When copying ,'c' The original content and by 'a' To cover , So it's impossible to copy . therefore , For overlapping blocks of memory ,memcpy Appear unsafe .
Now let's see memmove function , This function can realize the movement of memory blocks . from sourc Point to the location of the copy num A value of bytes to destination Point to memory block . It seems that an intermediate buffer is used for copying , allow destination and source overlap .
source Pointers and destination The underlying type of the object pointed to by the pointer is independent of the function itself , The result is a binary copy of the data ; The function will not be in source Detect any terminating null characters in , It will accurately copy num Bytes ; To avoid crossing the border ,destination and source The size of the array pointed to by the parameter , It should be at least num Bytes ;
destination Returned .
3、 Function realization
This function is implemented , It's very simple , Just open up a space and copy a copy of the original data . But we can try to open up as little space as possible .

When source The position pointed to is destination In front of , Let's start with the last data 5 Start copying , You can see that before copying , The data to be copied will not be overwritten

And when source The position pointed to is destination In the back of , From the first data 5 Start copying , This effect can also be achieved .

4、 Code testing
边栏推荐
- 《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
- Postgresql14安装及主从配置
- 接口调不通,如何去排查?没想到10年测试老鸟栽在这道面试题上
- Customized template in wechat applet
- 数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
- Tutorial on the principle and application of database system (060) -- MySQL exercise: operation questions 11-20 (IV)
- Tutorial on the principle and application of database system (061) -- MySQL exercise: operation questions 21-31 (V)
- 使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
- Merge table rows - three levels of for loop traversal data
- R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(使用参数xlim和ylim将标签添加到可视化图像的特定区域、指定标签线段并添加箭头)
猜你喜欢

DDoS protection with iptables

安全保障基于软件全生命周期-NetworkPolicy应用

Socket class understanding and learning about TCP character stream programming

After finishing, help autumn move, I wish you call it an offer harvester

DXF reading and writing: align the calculation of the position of the dimension text in the middle and above

【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)

Qt5开发从入门到精通——第一篇概述

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

7.依赖注入

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
随机推荐
Denial of service DDoS Attacks
R language uses dpois function to generate Poisson distribution density data and plot function to visualize Poisson distribution density data
Poj3275 ranking the cows
leetcode-深度优先与广度优先遍历
P1797重型运输 题解
Deploy application delivery services in kubernetes (Part 1)
Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
30天刷题计划(三)
7.依赖注入
Istio四之故障注入和链路追踪
[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
性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...
《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
Graph traversal (BFS & DFS basis)
What is the reason why the words behind word disappear when typing? How to solve it?
Three men "running away" from high positions in the mobile phone factory
Tutorial on the principle and application of database system (060) -- MySQL exercise: operation questions 11-20 (IV)
国产API管理工具Eolink太好用了,打造高效的研发利器
POJ1860货币兑换题解
浅谈WebSocket