If I have a function in program
int main(){
char *name = "New Holland";
modify(name);
printf("%s\n",name);
}
that calls this function
void modify(char *s){
char new_name[10] = "Australia";
s = new_name; /* How do I correct this? */
}
how can I update the value of the string literal name (which now equals new Holland) with Australia.
The problem I think that I face is the new_name is local storage, so after the function returns, the variable is not stored
Try this:
use strcpy/memcpy to bing a local array variable to an outer string literal.
Try this:
If you define
new_name
as an array then it will become a local variable, instead the above defines a pointer, to a string literal. Also, in C the parameters are passed by value, so you need to pass pointers to objects you want to modify.