当前位置:网站首页>strcpy的实现你知道吗?
strcpy的实现你知道吗?
2022-04-22 05:29:00 【南航科院小张】
一、代码解析
strcpy函数是将源头字符数组拷贝到目标字符数组中,注意源头字符数组中’\0’也拷过去了! ! !,把’\0’拷过去了,那么如果目标字符数组内容更长,那么因为把’\0’拷过去了,所以后面的内容都打印不了了!!!
注意目标字符数组的长度一定要大于源头字符数组,不然怎么装得下源头字符数组拷过来的字符串呢?注意断言!!!注意我的while循环怎么用的!!!注意函数返回值是char*的,传回去的是目标字符数组的首地址!!!不能让源头字符数组内容改变,所以加const!!!。
主要就是源头字符数组首地址内容开始拷到目标字符数组首地址内容里,然后两个指针同时加一个元素(指向下一个元素)地址;直到遇到’\0’,用循环嘛,条件是’\0’哦!!!
二、代码
#include <stdio.h>
#include <assert.h>
char* my_strcpy ( char* arr1,const char* arr2) {
assert(arr2 != NULL);
assert(arr1 != NULL);
char* str = arr1;
while (*arr1++ = *arr2++) {
;
}
return str;
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "ghilk";
printf("%s", my_strcpy(arr1, arr2));
return 0;
}
总结
如果有问题,还望评论区指正!!!
版权声明
本文为[南航科院小张]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_68844357/article/details/124332734
边栏推荐
- C language version: establishment and basic operation of two-way linked list
- [C] file operation
- New tips for JS in 2022
- 【无标题】GBase 8s V8.8 SQL 指南:教程-6
- Method for coexistence of Keil-C51 and keil arm
- unity接入ILRuntime之后 热更工程应用Packages下面的包方法 例如Unity.RenderPipelines.Core.Runtime包
- Network attack and Defense Security Learning Platform - upload key 3
- Meetup 02 review: Q & A highlights
- 深圳-西双版纳
- [WPF] converter
猜你喜欢

Unity built-in terrain optimization

Fundamentals of graphics - depth of field / DOF

MySQL installation and configuration - detailed tutorial

Pratique du langage C (2) - - mise en oeuvre de l'addition polynomiale par liste liée

Application and selection of it power distribution and fire current limiting protector

Fundamentals of graphics - screen spatial reflection (SSR)

Kaggle_NBME NLP比赛Baseline详解(2)

Sourcetree version backtracking and single change version backtracking

Method for coexistence of Keil-C51 and keil arm

Fundamentals of graphics | real time shadow rendering
随机推荐
Realization of mathematical function curve editor with minscript script language
【WPF】VisualStateManager
Codeforces Round #783 (Div. 2) D - Optimal Partition(dp/权值线段树 2100)
水处理控制系统采用信号隔离器解决因某些现场非电量安装条件的限制问题
Fundamentals of graphics - depth buffer
Efficiency tool | special screenshot auxiliary software pureref
How to use U deep boot U disk to clear the system login password
C language practice (2) -- polynomial addition with linked list
Fundamentals of graphics | real time shadow rendering
Meetup 02 review: Q & A highlights
Integer source code
Unreal engine sequence effect and audio binding trigger
Codeforces Round #783 (Div. 2) ABCD
Fundamentals of graphics - Mobile GPU architecture
Empty object mode (3.14-3.20)
Configure security policy on ENSP
Von Neumann architecture
Four startup modes of activity
Ezyslice cut characters
AssetBundle packaging based on unitygameframework framework