注册 登录
编程论坛 C++教室

strcpy函数式怎么使用的?求解具体的运行方式,

zoufengrui 发布于 2012-12-19 20:55, 6317 次点击
越详细越好,新手一枚,老师没有讲,也不准备讲……
9 回复
#2
yuccn2012-12-19 23:24
char str1[128] = { 0 };
char str2[128] = { "what the fuck!" };

strcpy(str1,str2); // 吧str2 拷贝到str1

没有再细了,1+1 = 2 没有那么多的为什么了
#3
额外覆盖2012-12-20 11:40
c++中不是有种′方式可以直接str1=str2吗?更简单
#4
aababy2012-12-20 11:55
strcpy(dest, src);
把src的所有内容复制到dest里面,
1. 首先你要有两个已分配内存的字符传, 已分配内存, 就是说dest不能像 char *dest; 这样就传到参数里面去了,
必须要 char *dest = malloc(128*sizeof(char)); 当然也可以 char str1[128] = {0}; 这是用栈内存.
2. 必须要保证 dest的长度大于等于 src的长度, 当然你可以直接给dest分配一个很长的长度, 如上面的128就是长度.

http://baike.baidu.com/view/1026861.htm
#5
zhuanjia02012-12-20 22:45
回复 3楼 额外覆盖
那是string类
#6
zhuanjia02012-12-20 22:46
MSDN
char *strcpy( char *strDestination, const char *strSource );

程序代码:



/* STRCPY.C: This program uses strcpy

 * and strcat to build a phrase.

 
*/

#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[80];
   strcpy( string, "Hello world from " );
   strcat( string, "strcpy " );
   strcat( string, "and " );
   strcat( string, "strcat!" );
   printf( "String = %s\n", string );
}
#7
zhuanjia02012-12-20 22:48
回复 3楼 额外覆盖
那是string类
#8
々NARUTO2012-12-21 22:34
自己 查MSDN 不就知道了
#9
yuccn2012-12-22 10:11
回复 3楼 额外覆盖
那个是用了类了,可以用=是 做了 =  的运算符重载了
#10
peach54602012-12-24 17:43
程序代码:
 * C语言标准库函数strcpy的一种典型的工业级的最简实现
  * 返回值:
  * 返回目标串的地址。
  * 对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。
  * 参数:
  * strDestination
  * 目标串
  * strSource
  * 源串
  ***********************/
  char *strcpy(char *strDestination,const char *strSource)
  {
  assert(strDestination!=NULL && strSource!=NULL);
  char *strD=strDestination;
  while ((*strDestination++=*strSource++)!='\0');
  return strD;
  }
百度百科
1