You can do it this way:
#include <stdio.h>
void changeStr(const char** str1)
{
*str1 = "foo";
}
int main()
{
const char* aStr = "abc123";
printf("%s\n", aStr);
changeStr(&aStr);
printf("%s\n", aStr);//aStr is still the same :(
return 0;
}
here :
void changeStr(char* str1) {
str1 = "foo";
}
str1
is a copy value that holds the address of "abc123"
, so any changes on it, it will be on the scope of the function. What you need is to modify the value of pointer itself, the holder of the address, so it points to "foo"
, that's why you need to pass char **
, pointer to pointer.