程序改错,请将程序中的错误改正过本程序的功能是将字符串s1复制为字符品 //ex08_5.cpp #include <iostream> using namespace std; int main() char s1[]="Goodbye",s2[8]; char *from=s1,*to=s2; while(*from){ *to++=*from++;} to='\0'; cout<<sl; cout<<s2; return 0;
时间: 2024-01-26 19:03:03 浏览: 19
程序存在以下错误:
1. 在第 5 行和第 6 行缺少了左括号;
2. 在第 8 行缺少了类型说明符 int;
3. 在第 10 行,将字符型指针 to 赋值为 '\0',应该赋值为字符串结束符 '\0'。
改正后的程序如下:
```c++
//ex08_5.cpp
#include <iostream>
using namespace std;
int main() {
char s1[] = "Goodbye", s2[8];
char *from = s1, *to = s2;
while (*from) {
*to++ = *from++;
}
*to = '\0';
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
```
改正后程序的功能是将字符串 s1 复制为字符数组 s2。