关于c++指针的程序

来源:百度知道 编辑:UC知道 时间:2024/06/07 14:30:01
#include <iostream.h>
char *strcpy(char *s1, const char *s2){
char *p = s1;
while(*s1++ = *s2++);
return p;
}
void main()
{
char *s; //为啥会出错,改成char s[10];就对了
cout<<strcpy(s, "test");
}

你的char *s,只是声明了一个char型指针,而没有为除s[0]外的元素分配内存空间。
也可改为 char *s = new char[10];

还有#include <iostream>下面要加上
using namespace std; 吧。。。

char *s ;
没有内存空间,应该new出来