当前位置:网站首页>my_strcpy的实现(经典,简单,实用,收藏)
my_strcpy的实现(经典,简单,实用,收藏)
2022-07-23 05:39:00 【rookieﻬ°】
C语言函数库中定义的strcpy是什么样的?


这是cpulspuls上面的注解(点击进入注解网页),大家感兴趣可以仔细进网站看看。
标准解释告诉我们,strcpy的返回类型为char*,也就是字符指针类型,相当于字符串数组首地址,而函数strcpy(arr1,arr2)的作用就是将arr2的内容copy到arr1中,但是arr2中的内容不变。
下面是cplusplus上举的例子:
/* 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;
}
输出:
str1: Sample string
str2: Sample string
str3: copy successful
--------------------------------------------------------------------------------------------------------------------------------------
如何快准狠写出自己的my_strcpy?
下面是我的方法,附带注解,望大佬指正!
#include<stdio.h>
char* my_strcpy(char* destination,const char* resource)//这个const可是非常重要的,可是非常重要的,因为arr2是我们不想修改的
{
char* ret = destination;//记住目标的地址,后面作为return的对象
while(*destination++=*resource++)//这里当*resource的内容不为\0的时候,其内容远远不断的赋值给
{
//arr1也就是目标字符串,最后当\0赋值完毕的时候,while()循环
; //判定条件为假,终止操作,这时我们的目的也已经达成。
}
return ret;//返回已经修改的字符串的首地址,相当于返回了修改的目标字符串
}
int main()
{
char arr1[] = {
"xxxxxxxxxxxx"};
char arr2[] = {
"jinitaimei"};
my_strcpy(arr1,arr2);//调用函数
printf("%s\n",arr1);
return 0;
}
朋友,我想,你也不想白嫖吧[doge]
边栏推荐
- 【无标题】
- 【uiautomation】键指令大全(以及三种调用方式)+常用鼠标动作+SendKeys+Inspect学习
- Paging and filtering
- Oracle创建数据库“监听程序未启动或数据库服务未注册”错误处理
- 遇到了ANR错误
- wps中的word中公式复制完后是图片
- WebSocket长连接
- Matrix vector derivation in machine learning
- [Doris]配置和基本使用contens系统(有时间继续补充内容)
- Large factory interview machine learning algorithm (0): Feature Engineering | data preprocessing
猜你喜欢

Scattered notes of machine learning: some concepts and notes

pyspark学习笔记

【Pyradiomics】提取的影像组学特征值不正常(很多0和1)

Pycharm occupies C disk

【文献调研】在Pubmed上搜索特定影响因子期刊上的论文

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

JDBC的学习以及简单封装

Getting started with RPC and thrift

When using cache in sprintboot, the data cannot be loaded
随机推荐
【pyradiomics】bugFix:GLCM特征时:IndexError: arrays used as indices must be of integer (or boolean) type
字典创建与复制
With only 5000 lines of code, AI renders 100 million landscape paintings on v853
[pytho-flask笔记5]蓝图简单使用
框架介绍mvt
pycharm占用c盘
Oracle创建数据库“监听程序未启动或数据库服务未注册”错误处理
Mysql-8.0.28 user operation or user permission Operation
RPC与thrift入门
【无标题】
[pytho-flask筆記5]藍圖簡單使用
Database topics necessary for interview
[Hudi]hudi的编译及hudi&spark和hudi&flink的简单使用
Redis数据库和项目框架
Mixed view base class
3.Flask 中的线程
Déploiement du projet (version abrégée)
Celery异步发送短信
Sequencing, current limiting
CountDownLatch的用法