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

strcpy()的实现问题

howema 发布于 2008-04-01 17:13, 670 次点击
#include<iostream>
#include<assert.h>
using namespace std;
char *strcpy(char *d, char *s);
int main()
{
char *a="hello";
char *b;
strcpy(b,a);
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
}
char *strcpy(char *d, char *s)
{
 
 assert((d!=NULL)&&(s!=NULL));
 char *t=d;
 while((*d++=*s++)!='\0')

 return t;
}

程序可以编译通过,但是运行的时候就出现段错误。请高手指教是什么问题!
1 回复
#2
herolzx2008-04-01 17:25
char *b 没有初始化,少分号
#include<iostream>
#include<assert.h>
using namespace std;
char *strcpy(char *d, char *s);
int main()
{
char *a="hello";
char temp[1234]={0};
char *b = temp;
strcpy(b,a);
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
system("pause");
}
char *strcpy(char *d, char *s)
{

assert((d!=NULL)&&(s!=NULL));
char *t=d;
while( (*d++= *s++) != '\0');//看这里



return t;
}

[[it] 本帖最后由 herolzx 于 2008-4-1 17:39 编辑 [/it]]
1