当前位置:网站首页>my_ Implementation of strcpy (classic, simple, practical, collection)
my_ Implementation of strcpy (classic, simple, practical, collection)
2022-07-23 11:19:00 【rookieﻬ°】
List of articles
C Defined in the language function library strcpy What does it look like ?


This is a cpulspuls The note above ( Click to enter the annotation page ), If you are interested, you can go into the website carefully .
The standard explanation tells us ,strcpy The return type of is char*, That is, the character pointer type , Equivalent to the first address of the string array , The function strcpy(arr1,arr2) Is that take arr2 The content of copy To arr1 in , however arr2 The contents of the remain unchanged .
Here is cplusplus The above example :
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
Output :
str1: Sample string
str2: Sample string
str3: copy successful
--------------------------------------------------------------------------------------------------------------------------------------
How to write your own quickly, accurately and ruthlessly my_strcpy?
Here's my approach , With notes , I hope you can correct me !
#include<stdio.h>
char* my_strcpy(char* destination,const char* resource)// This const But very important , But very important , because arr2 It's something we don't want to change
{
char* ret = destination;// Remember the address of the target , Later as return The object of
while(*destination++=*resource++)// Here *resource The content of is not \0 When , Its content is assigned to
{
//arr1 That is, the target string , Finally, when \0 When the assignment is finished ,while() loop
; // The judgment condition is false , Termination operation , At this time, our goal has been achieved .
}
return ret;// Return the first address of the modified string , It is equivalent to returning the modified target string
}
int main()
{
char arr1[] = {
"xxxxxxxxxxxx"};
char arr2[] = {
"jinitaimei"};
my_strcpy(arr1,arr2);// Call function
printf("%s\n",arr1);
return 0;
}
friend , I think , You don't want to go whoring for nothing [doge]
边栏推荐
猜你喜欢
随机推荐
systemctl-service服务添加环境变量及模板
构造函数,原型链,instanceOf
[python flask notes 5] blueprint is easy to use
从零开始的pytorch小白使用指北
some、every、find、findIndex的用法
shell/sh/bash的区别和基本操作
Constructor, prototype chain, instanceof
频谱聚类|拉普拉斯矩阵
2. Analysis of the return value of the startup function
Matrix vector derivation in machine learning
Celery异步发送短信
大厂面试机器学习算法(6)时间序列分析
Pywinauto+某应用程序(学习至第9讲)--受阻
plsql创建Oracle数据库报错:使用Database Control配置数据库时,要求在当前Oracle主目录中配置监听程序 必须运行Netca以配置监听程序,然后才能继续。或者
同步发送短信验证码
SQL常见面试题目与答案整理
Redis database and project framework
When using cache in sprintboot, the data cannot be loaded
Five methods to prevent over fitting of neural network
Inheritance mode of JS








![[部署]presto-server-0.261.tar.gz的集群部署和启动](/img/37/1185b2321b003a7793c8c37891008c.png)
