這裏是我試過的代碼:爲什麼我不能用一個動態分配的數組覆蓋一個數組(它傳遞給另一個函數)?
void check(char*, int);
int main(){
int len;
char* str="stack over flow";
len=strlen(str);
check(str,len);
getch();
return 0;
}
void check(char* str,int len)
{
char* arr;int i=0;
arr=(char*)malloc(sizeof(char)*len);
arr="over flow stack";
while(arr[i]!='')
{
str[i]=arr[i];//here the error is thrown
i++;
}
puts(str);
}
當我試圖打印回改編的元素融入STR我收到提示。
[請參閱爲什麼不投的malloc'的返回值()在'討論'和家庭C'](http://stackoverflow.com/q/605845/2173917)。 –
提示:'str [i]'不可修改。它指向一個字符串文字。 –
請格式化您的代碼。 –