当前位置:网站首页>C language: Simulated Implementation of library function strcpy
C language: Simulated Implementation of library function strcpy
2022-06-13 09:11:00 【Caixinzhi】
How to write a function to simulate the implementation of library functions strcpy Well ?
First , We need to know strcpy The function of this function 、 What are the usages , here , I recommend a website for inquiry , Name is :cplusplus, website :cplusplus.com - The C++ Resources Networkhttp://www.cplusplus.com/
This website is all in English , If you don't want to read English , You can also see the corresponding Chinese website , Name is :cppreference, website : cppreference.comhttps://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5
Next , Let's just open a website to check strcpy, So here I'm going to use theta cplusplus, among , I have written the important information in the picture , You can refer to it :
here , We already know that strcpy How to call a function , Then the next programming will become very simple , In the code written below , I also have three levels , Gradually optimize the library functions to be simulated strcpy Code for ( Simulate the implementation of library functions strcpy The function name of is temporarily called my_strcpy):
1. primary : The first stage is very direct , Byref -> In exchange for -> Default return
#include <stdio.h>
void my_strcpy(char* src, char* dest) // Simulate the implementation of library functions strcpy ( primary )
{
while (*src != '\0')
{
*dest = *src;
src++;
dest++;
}
*dest = *src;
}
int main()
{
char* ch = "abcde";
char* copy[20] = {0};
my_strcpy(ch, copy);
printf("%s\n", copy);
return 0;
}
2. intermediate : Optimization of the primary , Make it more rigorous , At the same time, it makes the code look more concise
#include <stdio.h>
#include <assert.h>
void my_strcpy(char* src, char* dest) // Simulate the implementation of library functions strcpy ( intermediate )
{
assert(src != NULL && dest != NULL);
while (*dest++ = *src++)
{
;
}
}
int main()
{
char* ch = "abcde";
char* copy[20] = { 0 };
my_strcpy(ch, copy);
printf("%s\n", copy);
return 0;
}
here , We used the previously introduced assert Function to assert , If you make a null pointer , Compile to run , But the result will prompt an error . This makes the code more rigorous , It is also convenient for us to find out the mistakes in time . Besides , We will also while The statement is changed to post ++ In the form of , hold while The statement block in becomes an empty statement , Actually , This idea is roughly the same as the previous one , It's just a further simplification .
3. senior : We should be more rigorous about the intermediate level
#include <stdio.h>
#include <assert.h>
void my_strcpy(char const * src, char* dest) // Simulate the implementation of library functions strcpy ( senior )
{
assert(src && dest);
while (*dest++ = *src++)
{
;
}
}
int main()
{
char* ch = "abcde";
char* copy[20] = { 0 };
my_strcpy(ch, copy);
printf("%s\n", copy);
return 0;
}
Compared with the above code , There's one more here const, So this const What's the use ?
Here is a brief introduction ,count It is used to protect parameters ,const There are two cases of decorating pointers :
One is ,const Put it in * Left side , It modifies what the pointer points to , Indicates that the content pointed to by the pointer cannot be changed by the pointer , But the pointer variable itself can be changed .
Two is ,const Put it in * To the right of , The modification is the pointer variable itself , The contents of pointer variables cannot be changed , But what the pointer points to can be changed by the pointer .
Through to const After understanding , We can see in the code *src In front of the const, Description is the first case , The content pointed to by the pointer cannot be changed by the pointer , This pair of while Statement conditions play an important role in , If it is not written as the above code , It's the reverse , become :*src++ = *dest++, An error will be reported immediately after running , because const modification *src, bring *src Can't be changed , But this will change the *dest Assign a value to *src, Of course it will be wrong . The advantage of writing like this is , Standardize your initial ideas , It won't lead to chaos behind. I don't know which one to change , Which situation should not be changed .
This article is quite long , A lot of words , But there are many knowledge points , And continuous improvement and optimization of the same code , Hopefully that helped .
边栏推荐
- 20220606 关于矩阵的Young不等式
- C language 7-13 day K candle chart (15 points)
- 20211108 observable, controllable, stable and measurable
- 【QNX Hypervisor 2.2 用户手册】4.5.1 构建QNX Guest
- 20211020 段院士全驱系统
- 简单实现数据库链接池
- Simulink的Variant Model和Variant Subsystem用法
- Simple implementation of database link pool
- CAS NO lock
- 15. copy constructor
猜你喜欢
How many TCP connections can a machine create at most?
Onnx crop intermediate node
Class loading overview
Yolov5 face learning notes
Collection of garbled code problems in idea development environment
线上调试工具Arthas高级
torch. How to calculate addmm (m, mat1, mat2)
教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5
Cisco, Huawei network equipment
Longadder of the source code of JUC atomic accumulator
随机推荐
Batch read all voice files under the folder
Collection of various books
How to become a white hat hacker? I suggest you start from these stages
JUC原子引用与ABA问题
Neo4j环境搭建
Can I open an account for the reverse repurchase of treasury bonds? Can I directly open the security of securities companies on the app for the reverse repurchase of treasury bonds? How can I open an
类的加载概述
教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5
Vscode plug in
【QNX Hypervisor 2.2 用户手册】4.5.1 构建QNX Guest
Yolov5 face learning notes
Drill down to protobuf - Introduction
pytorch相同结构不同参数名模型加载权重
Sonar scan ignores the specified file
简单实现数据库链接池
Lecture par lots de tous les fichiers vocaux sous le dossier
JUC atomic integer
如何成为白帽子黑客?我建议你从这几个阶段开始学习
QML(06)——qml. Add a new folder under QRC
Pytorch same structure different parameter name model loading weight