Let’s suppose we have a function like this:
int change_char( char *str, const char a, const char b) {
int n = 0;
if ( str ) {
while ( *str ) {
if ( *str == a ) {
*str = b; // Can I modify the value pointed by the pointer?
++n;
}
++str; // Can I modify the pointer?
}
}
return n;
}
When we call that function, like in the following sample, the pointer is passed by value, so you can’t modify the original pointer (only its local copy), but you can modify the value of the object pointed by that pointer:
char test[] = "Hello world!";
change_char(test, 'o', 'X');
printf("%s\n",test); // it will output HellX wXrld!
2
solved How to change the value of pointer in C [closed]