当前位置:网站首页>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:00 【GD_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,关注一下,下期更精彩.
边栏推荐
猜你喜欢
随机推荐
推荐系统-排序层-2018:xDeepFM模型【xDeepFM是对DCN(Deep&Cross)的改进,不是对DeepFM的改进】【中科大】
TiDB | 来说说TiEM初体验吧
Process finished with exit code -1073740791 (0xC0000409)
unity 使用paint in 3d实现画板效果
pacman包 管理各种R包
CSDN官方插件
ResourceQuota 和 LimitRange 实践
数据库中的外键是否必要存在
WinForm(三)揭开可视化控件的面纱
高并发、多线程、分布式都不懂,你拿什么跳槽阿里、腾讯、京东?还不好好学习啊
Chapter 13 Bayesian Network Practice
Unity Animator动画与状态机
记录自己LitJson解析Json的方法
自用工具 猴子都会用的UNITY文件浏览器(浏览文件夹)
Day020 Method Overriding and Polymorphism
C 基础语法2 —— 数组
解决你的R语言乱码问题
More than 40 a joke
How can machinery manufacturing companies use ERP systems to manage production schedules?
Unity NavMesh基础自动寻路









