C语言:strcpy

char *strcpy(char *restict dst,const char *restrict src);dst是目的,src是原

这会把src的字符串拷贝到dst

restrict表示src和dst不重叠

返回dst

char *dst=(char*)malloc(strlen(src)+1);

strcpy(dst,src);

#include <stdio.h>

#include <string.h>

char* mycpy(char* dst,const char* src)

{

//int idx = 0;

//while(src[idx]){

//dst[idx]=src[idx];

//idx++;

//}

//dst[idx] = '\0';

char*ret = dst;

while(*src !='\0'){

*dst = *src;

dst++;

src++;

}

*dst ='\0';

return ret;

}

int main(int argc,char const *argv[])

{

char s1[] = "abc";

char s2[] = "abc";

mycpy(s1,s2);

printf("%d %d",s1,s2);

return 0;

}

相关推荐
双叶8362 分钟前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)(网页版预告)(html)(js)(json)
c语言·javascript·数据结构·html·json
子豪-中国机器人12 分钟前
C++ 蓝桥 STEMA 省选拔赛模拟测试题(第一套)
开发语言·c++·算法
酷炫码神22 分钟前
C#数组与集合
开发语言·c#
英英_23 分钟前
python 爬虫框架介绍
开发语言·爬虫·python
钢铁男儿24 分钟前
C# 深入理解类(静态函数成员)
java·开发语言·c#
大模型铲屎官1 小时前
【Python-Day 14】玩转Python字典(上篇):从零开始学习创建、访问与操作
开发语言·人工智能·pytorch·python·深度学习·大模型·字典
yunvwugua__2 小时前
Python训练营打卡 Day27
开发语言·python
Java致死2 小时前
设计模式Java
java·开发语言·设计模式
zh_xuan3 小时前
c++ 类的语法3
开发语言·c++
belldeep6 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言