int find_replace_str(char str[],const char find_str[],const char replace_str[])
{
//别的操作
char *newStr=new char[newLength+1];
//对newStr的操作
str = newStr;
return count;
}
int main()
{
char *str="abceeeabceeeeabceeeeabceeeeeabc";
const char *find_str="abc";
const char *replace_str="ABCD";
cout<<find_replace_str(str,find_str,replace_str)<<" "<<str<<endl;
cin.get();
return 0;
}
main函数中的str还是之前的str,请教大神如何解决
我知道传递引用或者二级指针可以,但是这是南京大学复试的题目,函数名就是这么写的。
There are two problems in the program:
So, the array allocated in
str
can be passed to the functionmain
through the parameterfind_replace_str
, and the functionfind_replace_str
then modifies and replaces the array, thus affecting the array allocated inmain
content.In addition, it is wrong to assign a constant string to a non-const pointer at
(2)
. A constant string can only be assigned to a constant pointer or a character array. Otherwise, it would be possible to modify the string through a non-const pointer.The meaning of the function name is very obvious. For character array operations, replace the substring that needs to be replaced.
You wrote the main function, but it is wrong to use it this way. The string is const char * and cannot be changed. You need to declare the array
Like this, in find_replace_str