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;
}