Simulate the implementation of strcpy function (including multiple optimization ideas)

Today, I will bring you the independent implementation of strcpy function. Without much talk, let's start.

Statement: The following picture for the description of the strcpy function is from the software MSDN

Introduction to strcpy function

Let's see the following picture (from software MSDN)

Click MSDN software, click Index, and search for strcpy in the following frame to understand the function of strcpy. Turn it down to find the sentence that parses the function, as follows:

In translation, strcpy copies strSource, including termination symbols ( 0), to strDestination. strSource and strDestination are some of the contents of the first figure, as follows:

Through the above analysis, we roughly know the function of strcpy. Next, we will try to implement this function.

Simple implementation of strcpy function

We create character arrays and implement strcpy functions through pointers.

#include<stdio.h>
void my_strcpy (char* arr1,char* arr2)
{
    while(*arr2!='\0')
	{
		//Note that it is post plus
	    *arr1++ = *arr2++;
	}
	//Copy  0
	*arr1 = *arr2;
}
int main ()
{
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	my_strcpy(arr1,arr2);                     //We need to copy arr2 to arr and copy the library function strcpy to put arr2 behind arr1
	printf("%s\n",arr1);                     //Print arr1 observation copy results
	return 0;
}

Copy content and copy 0 simultaneously

As the code of the above program is implemented, copying content and copying 0 are performed separately. We will optimize them here.

#include<stdio.h>
void my_strcpy (char* arr1,char* arr2)
{
	//When copying  0 is completed, 0 is returned, which is judged as false, and the cycle ends
    while(*arr1++ = *arr2++)
	{
		;
	}
}
int main ()
{
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	my_strcpy(arr1,arr2);                     //We need to copy arr2 to arr and copy the library function strcpy to put arr2 behind arr1
	printf("%s\n",arr1);                     //Print arr1 observation copy results
	return 0;
}

Depth analysis of strcpy function 1


From the above picture, we can see that const is added to the copied character array, but not to the overwritten character array. Next, we need to understand the function of const.

The function const is a keyword, which is immutable.

const is added in front of strsource to prevent the source from being modified when the strcpy library function is implemented.

It can be seen that our code is also optimized.

#include<stdio.h>
//Give the source immutability to prevent errors caused by * arr1++and * arr2 location errors
void my_strcpy (char* arr1,const char* arr2)
{
	//When copying  0 is completed, 0 is returned, which is judged as false, and the cycle ends
    while(*arr1++ = *arr2++)
	{
		;
	}
}
int main ()
{
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	my_strcpy(arr1,arr2);                     //We need to copy arr2 to arr and copy the library function strcpy to put arr2 behind arr1
	printf("%s\n",arr1);                     //Print arr1 observation copy results
	return 0;
}

Depth analysis of strcpy function 2


It can be seen from the picture that the strcpy library function has a return value, so we need to set the return value when simulating the strcpy function.

#include<stdio.h>
//Give the source immutability to prevent errors caused by * arr1++and * arr2 location errors
char* my_strcpy (char* arr1,const char* arr2)
{
	//Save arr1 at the beginning, because arr1 is no longer the address of the first element
	char* ret = arr1;
	//When copying  0 is completed, 0 is returned, which is judged as false, and the cycle ends
    while(*arr1++ = *arr2++)
	{
		;
	}
	return ret;
}
int main ()
{
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	printf("%s\n",my_strcpy(arr1,arr2));                //Print arr1 observation copy results
	return 0;
}

Introduce assert ion

We also need to consider that our strSource and strDestination cannot be passed in null pointers, and referencing assert can report errors in a timely manner.

#include<stdio.h>
#include<assert.h>
//Give the source immutability to prevent errors caused by * arr1++and * arr2 location errors
char* my_strcpy (char* arr1,const char* arr2)
{
	char* ret;
	assert(arr1!=NULL);
	assert(arr2!=NULL);
	//Save arr1 at the beginning, because arr1 is no longer the address of the first element
	ret = arr1;
	//When copying  0 is completed, 0 is returned, which is judged as false, and the cycle ends
    while(*arr1++ = *arr2++)
	{
		;
	}
	return ret;
}
int main ()
{
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	printf("%s\n",my_strcpy(arr1,arr2));                //Print arr1 observation copy results
	return 0;
}

This is the final version of the strcpy function optimization. I like to click a little heart and pay attention to it. The next issue will be more exciting.

Tags: C++ programming language

Posted by jamesflynn on Tue, 20 Sep 2022 01:41:48 +0930