当前位置:网站首页>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 .

原网站

版权声明
本文为[Caixinzhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270533005544.html