我寫了一個(gè)MyStrcpy函數(shù),具體如下:#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;}會(huì)報(bào)錯(cuò):錯(cuò)誤為error C2105: '++' needs l-value當(dāng)然s2輸出也不對,求教原因.
1 回答

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