Home  >  Article  >  Backend Development  >  Learn how to use the "strcpy()" function in two minutes

Learn how to use the "strcpy()" function in two minutes

烟雨青岚
烟雨青岚forward
2020-07-03 13:40:074813browse

Learn how to use the

c语言strcpy()用法:

strcpy,即string copy(字符串复制)的缩写。

strcpy是一种C语言的标准库函数,strcpy把从src地址开始且含有’\0’结束符的字符串复制到以dest开始的地址空间,返回值的类型为char*。

通俗解释

定义一个字符串char a[20],和一个字符串c[]=“i am a teacher!”;

把c复制到a中就可以这样用:strcpy(a,c);

这个函数包含在头文件 bbed3fed50f96ac7490cfc6a498c4bc5中.

C语言标准库的写法

//C语言标准库函数strcpy的一种典型的工业级的最简实现。
 
//返回值:目标串的地址。
 
//对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。
 
//参数:des为目标字符串,source为原字符串。
 
char* strcpy(char* des,const char* source)
 
{
 
 char* r=des;
   
  assert((des != NULL) && (source != NULL));
 
 while((*r++ = *source++)!='\0');
 
 return des;
 
}
 
//while((*des++=*source++));的解释:赋值表达式返回左操作数,所以在赋值'\0'后,循环停止。

用法

这是C语言里面复制字符串的库函数, 函数声明包括在专门处理字符串的头文件bbed3fed50f96ac7490cfc6a498c4bc5中:

char * strcpy( char * dst, const char * src );

这个函数把字符串src复制到一分配好的字符串空间dst中,复制的时候包括标志字符串结尾的空字符一起复制。操作成功,返回dst,否则返回NULL.

示例程序

#include
#include 
void main()
{
	char a[20], c[] = "I am a teacher!";
	strcpy(a, c);
	printf(" c=%s\n", c);
	printf(" a=%s\n", a);
}

感谢大家的阅读,希望大家收益多多。

本文转自: https://blog.csdn.net/mao_hui_fei/article/details/84642447

推荐教程:《C语言

The above is the detailed content of Learn how to use the "strcpy()" function in two minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete