当前位置:网站首页>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 .
边栏推荐
- How excel adds hyperlinks to some text in a cell
- Use of grep
- Yolov5 face learning notes
- Detailed explanation of C language callback function
- C/s model and P2P model
- JUC atomic array
- Pytorch same structure different parameter name model loading weight
- C language time difference calculation
- Overview of common layers of image recognition neural network (under update)
- 如何成为白帽子黑客?我建议你从这几个阶段开始学习
猜你喜欢
随机推荐
Yolov5 face video stream
Collection of various books
20211006 积分、微分、投影均属于线性变换
Immutability of shared model
Talking about acid of database
Exporting MySQL data table documents using Navicat
Neo4j environment construction
20211108 observable, controllable, stable and measurable
Overview of common layers of image recognition neural network (under update)
Redis fuzzy query batch deletion
【安全】零基礎如何從0到1逆襲成為安全工程師
Simulink variant model and variant subsystem usage
JUC原子数组
共享模型之不可变
C language 7-13 day K candle chart (15 points)
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
an error occurred while trying to rename a file in the destination directory code 5
""? "& in URL Role of "" sign
Four kinds of hooks in deep learning
Vscode plug in