I made a function which change string, see the following code.
void Test(char* str, char c) {
str[1] = c;
}
int main(){
Test("Hi", '2');
}
I notice it made some run time error. I know how to prevent the error.
char buff[3] = "Hi";
Test(buff,'2');
but I don't know why the first example made run time error. I guess, if I pass string directly, it becomes const char. Does anyone explain what happened exactly?
ps. what if I use char* str = "hi", then pass it into the argument?
char* buff = "Hi";
Test(buff,'2');
like this. Can I modify buff?
const char*.char *buff, but still no. Using a pointer to non-constant data to point to constant data will never work regardless of how you try to get around it.char* str = "hi".