当前位置:网站首页>How to simulate the implementation of strcpy library functions
How to simulate the implementation of strcpy library functions
2022-07-28 06:48:00 【JuLiJuLi.】

First of all, let's take a look at strcpy The return value and parameters of this library function , The return value is char* Type is an address , There are two parameters , One is char* The other is const Embellished char*, First, let's see how this library function is used .
#include<stdio.h>
#include<string.h> //strcpy Library function header file
int main()
{
char ch1[20] = { 0 };
char ch2[] = "abcdef"; // It was said that strcpy The return value is char* Of , I'm going to use a char* To receive ,
char* flag = strcpy(ch1, ch2); //strcpy Contains two parameters , The previous one is the target string , Followed by the source string
printf("%s\n", flag); // As long as you get the first address of the copied string, you can print the whole string
return 0;
}
If you want to simulate strcpy This library function , Here are some things to know :
1. The source string must be in '\0' end .
2. In the source string '\0' Copy to target space .
3. The target space has to be large enough , Make sure you can store the source string .
3. The target space has to be variable .
#include<stdio.h>
#include<assert.h> //assert Assertion header file
char* My_strcpy(char* s1,const char* s2)
{
assert(s1 && s2); // Assert , prevent s1 also s2 One is a null pointer , If its condition returns an error , The program execution is terminated
char* flag = s1; // Record first s1 The starting position of this string , Prevent the following program from changing the position , Starting position not found
while (*s2!='\0') // Judge *s2 Whether it points to '\0', If not, copy , Yes, out of the loop
{
*s1 = *s2; // hold *s2 Copy the character pointed to to to *s1 The location of
s1++; // After copying, move to the next location to be copied in the target space
s2++; // After copying, move to the next position of the source string
}
*s1 = *s2; //strcpy The source string will be '\0' Also move past , But the above cycle meets '\0' It's going to jump out , So we need to assign again '\0' Copy it in, too
return flag; //strcpy The return value is char* Of , So directly return the first address of the copied string
}
int main()
{
char ch1[20] = { 0 };
char ch2[] = "abcdef";
My_strcpy(ch1, ch2);
printf("%s\n", ch1);
return 0;
}
Finally, you can optimize the function interior ,
char* My_strcpy(char* s1, const char* s2)
{
assert(s1 && s2);
char* flag = s1;
while (*s2 != '\0')
{
*s1 = *s2;
s1++;
s2++;
}
*s1 = *s2;
return flag;
}
char* My_strcpy(char* s1, const char* s2)
{
assert(s1 && s2);
char* flag = s1;
while (*s1++ = *s2++) // As long as the value of this expression is not 0; Will execute *s2 Assign a value to *s1, After ++ To move the location of the copy ,
{ // final '\0' It will also be assigned to s1, Then jump out of the loop , In this way, there is no need to add '\0' Copy to the target string
;
}
return flag;
}边栏推荐
- yapi漏洞挂马程序chongfu.sh处理
- Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
- Mysql-8.0.17-winx64 (additional Navicat) manual configuration version installation
- @PostConstruct注解及用处示例
- OJ 1284 counting problem
- 动态规划--简单题型之爬楼梯
- [pta-- use queues to solve the problem of monkeys choosing kings]
- Valgrind tool
- 【C笔记】数据类型及存储
- Question brushing record -- binary tree
猜你喜欢

Personal understanding of Chinese remainder theorem

STM32的IAP跳转相关bug经历

Graphic pipeline foundation (part outside)
![[hash table basics]](/img/8f/54a4780a02f81e5de3d92c25248e1e.png)
[hash table basics]

rancher部署实战

archery数据库审核平台部署

RayMarching实现体积光渲染

Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree

关于Shader KeyWord的整理
![[队列,栈的简单应用----包装机]](/img/bc/617b1eb35558c4f948018f593a1de5.jpg)
[队列,栈的简单应用----包装机]
随机推荐
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
[dynamic planning -- the best period for buying and selling stocks series 3]
OJ 1507 deletion problem
Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器
OJ 1451 digital games
[dynamic planning -- the best period series for buying and selling stocks]
Network communication and tcp/ip protocol
动态规划--简单题型之爬楼梯
Personal understanding of Chinese remainder theorem
explain详解
[dynamic planning -- the best period for buying and selling stocks Series 2]
[untitled]
redis实现分布式锁思路及redission分布式锁主流程分析
[C language] custom structure type
ZOJ Problem 1005 jugs
[队列,栈的简单应用----包装机]
Skimming records -- sequence traversal of binary tree
OJ 1505 保险丝
NIO示例