By using a double pointer as argument in a function, the program below can modify the string, I understand.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void display(char** output)
{
printf("%s\n", *output);
*output = "This is another test";
}
int main(void)
{
char* str = "This is a test";
display(&str);
printf("After the call: %s\n", str);
return 0;
}
However, I don't understand why using a single pointer as argument like below can't modify the string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void display(char* output)
{
printf("%s\n", output);
output = "This is another test";
printf("%s\n", output);
}
int main(void)
{
char* str = "This is a test";
display(str);
printf("After the call: %s\n", str);
return 0;
}
You may imagine the function and its call in the second shown program the following way
That is the argument expression
stris used to initialize the function local variableoutput. And within the function it is its local variableoutputthat is being changed. The pointerstrused as an initializer of the function local variable stays unchanged. The function does not deal withstrexcept of using it to initialize its parameteroutput.To change an object within a function you need to pass it to the function by reference.
In C passing by reference means passing an object indirectly through a pointer to it. Thus dereferencing the pointer the function has a direct access to the object pointed to by the pointer and can change it. And your first program demonstrates this approach.
Dereferencing the pointer
outputthe function has a direct access to the pointer
strdeclared in main and passed to the function through a pointer to itand changes it.