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(srcidx){

//dstidx=srcidx;

//idx++;

//}

//dstidx = '\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;

}

相关推荐
chenbingjie_c26 分钟前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
Escalating_xu2 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
实心儿儿2 小时前
Qt — Qt 事件
开发语言·qt
周杰偷奶茶2 小时前
【Java】数据类型与变量
java·开发语言
code斗2 小时前
Java数据结构:堆详解
java·开发语言·数据结构
重生之小比特2 小时前
【C++进阶】map和set
java·开发语言·c++
lie..3 小时前
30天从零开始学AI应用开发(Day 12):像产品一样稳:流式输出、报错重试、花钱控制
开发语言·人工智能·php
傻啦嘿哟3 小时前
Python的默认参数把我坑惨了,原来写[]和写None的区别这么大
开发语言·python·机器学习
阿钱真强道3 小时前
20 嵌入式操作系统 | ubus:把自己的程序状态暴露出去
c语言·进程通信·ubus·ubusd·blobmsg
DeepAgent3 小时前
AI Agent 工程实践(49):一次真实优化——从 Agent v1 到 v2
开发语言·人工智能·agent