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;

}

相关推荐
踏着七彩祥云的小丑3 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid3 小时前
Python12(网络编程)
开发语言·网络·php
W23035765734 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
.Ashy.4 小时前
2026.4.11 蓝桥杯软件类C/C++ G组山东省赛 小记
c语言·c++·蓝桥杯
Y4090014 小时前
【多线程】线程安全(1)
java·开发语言·jvm
2401_892070984 小时前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
不爱吃炸鸡柳4 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
minji...5 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
Dxy12393102165 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert