我寫了一個MyStrcpy函數,具體如下:#include <iostream> using namespace std;void MyStrcpy(char *s2, char *s1){while(*s1){*s2 = *s1;s2++;s1++;}*s2 = '\0';}int main() { char s1[] = "12345678";char s2[8] ={0};MyStrcpy(s2, s1);cout << s2 << endl;return 0;}但是如果改成#include <iostream> using namespace std;int main() { char s1[] = "12345678";char s2[8] ={0};while(*s1){*s2 = *s1;s2++;s1++;}*s2 = '\0';cout << s2 << endl;return 0;}會報錯:錯誤為error C2105: '++' needs l-value當然s2輸出也不對,求教原因.
1 回答

臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
char s1[] = "12345678";說明 s1是數組名, 數組名是常量, 不能夠進行后面的s1++這種自增操作的, 如果想的話, 可以
char* temp = s1;
然后
while(*temp)
{
*s2 = *temp;
s2++;
temp++;
}
- 1 回答
- 0 關注
- 725 瀏覽
添加回答
舉報
0/150
提交
取消