当前位置:网站首页>Simulate the realization of strcpy function (including multiple optimization ideas)

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

2022-08-06 07:37:20GD_small_bit

I bring it to you todaystrcpyAutonomous implementation of functions,话不多说,直接开始.



声明:下面对strcpyA picture taken from the function description,from the softwareMSDN



strcpy函数的介绍

Let's look at the picture below(来自软件MSDN
请添加图片描述
点开MSDN软件,Click Index again,And search in the border belowstrcpy来了解strcpy函数的作用,往下面翻,Find the sentence that parses the function,如下:
请添加图片描述
翻译过来就是,strcpy将strSource,包括终止符号(\0),复制到strDestination,而strSource和strDestinationHere are some of the contents of the first picture,如下:
请添加图片描述
通过上面的分析,我们大致知道了strcpy的作用,接下来,Let's try to implement this function.



简单实现strcpy函数

We create the character array,And through the pointer to achievestrcpy函数.

#include<stdio.h>
void my_strcpy (char* arr1,char* arr2)
{
    
    while(*arr2!='\0')
	{
    
		//Note that it is added after the addition
	    *arr1++ = *arr2++;
	}
	//拷贝\0
	*arr1 = *arr2;
}
int main ()
{
    
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	my_strcpy(arr1,arr2);                     //我们要实现arr2拷贝到arr,Fake library functionsstrcpy把arr2放在arr1后面
	printf("%s\n",arr1);                     //打印arr1Observe the copy results
	return 0;
}

Copy content and copy\0同时进行

Because the code of the above program is executed,Copy content and copy\0是分开进行的,We optimize here.

#include<stdio.h>
void my_strcpy (char* arr1,char* arr2)
{
    
	//当拷贝\0完成后,返回0,判断为假,循环结束
    while(*arr1++ = *arr2++)
	{
    
		;
	}
}
int main ()
{
    
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	my_strcpy(arr1,arr2);                     //我们要实现arr2拷贝到arr,Fake library functionsstrcpy把arr2放在arr1后面
	printf("%s\n",arr1);                     //打印arr1Observe the copy results
	return 0;
}


strcpyIn-depth analysis of functions1

请添加图片描述
We can see from the above picture,The copied character array is prependedconst,And the overwritten character array doesn't,接下来,我们要了解const的作用.

函数const是关键字,Give immutability.

const加在了strsource前面,prevent it from being implementedstrcpy库函数时,Modify the source.

由此可见,Our code is also optimized.

#include<stdio.h>
//Gives the source immutability,防止*arr1++和*arr2The wrong location results in an error
void my_strcpy (char* arr1,const char* arr2)
{
    
	//当拷贝\0完成后,返回0,判断为假,循环结束
    while(*arr1++ = *arr2++)
	{
    
		;
	}
}
int main ()
{
    
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	my_strcpy(arr1,arr2);                     //我们要实现arr2拷贝到arr,Fake library functionsstrcpy把arr2放在arr1后面
	printf("%s\n",arr1);                     //打印arr1Observe the copy results
	return 0;
}

strcpyIn-depth analysis of functions2

请添加图片描述
由图片可以知道,strcpy库函数是有返回值的,So we are simulatingstrcpyThe function needs to set the return value.

#include<stdio.h>
//Gives the source immutability,防止*arr1++和*arr2The wrong location results in an error
char* my_strcpy (char* arr1,const char* arr2)
{
    
	//save at the beginningarr1,因为后面arr1No longer the first element address
	char* ret = arr1;
	//当拷贝\0完成后,返回0,判断为假,循环结束
    while(*arr1++ = *arr2++)
	{
    
		;
	}
	return ret;
}
int main ()
{
    
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	printf("%s\n",my_strcpy(arr1,arr2));                //打印arr1Observe the copy results
	return 0;
}

引入断言assert

We also need to think about oursstrSource和strDestinationA null pointer cannot be passed,引用assertErrors can be reported in time.

#include<stdio.h>
#include<assert.h>
//Gives the source immutability,防止*arr1++和*arr2The wrong location results in an error
char* my_strcpy (char* arr1,const char* arr2)
{
    
	char* ret;
	assert(arr1!=NULL);
	assert(arr2!=NULL);
	//save at the beginningarr1,因为后面arr1No longer the first element address
	ret = arr1;
	//当拷贝\0完成后,返回0,判断为假,循环结束
    while(*arr1++ = *arr2++)
	{
    
		;
	}
	return ret;
}
int main ()
{
    
    char arr1[] = "*******************";
	char arr2[] = "hello world";                             
	printf("%s\n",my_strcpy(arr1,arr2));                //打印arr1Observe the copy results
	return 0;
}

This is finalstrcpyThe final version of function optimization is out,Like a little red heart,关注一下,下期更精彩.

原网站

版权声明
本文为[GD_small_bit]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/218/202208060726332628.html

随机推荐