I wonder the difference of same char* between when it resides in struct and in main() function.
Here is the code:
struct student {
char* name;
};
int main() {
// char* in struct
struct student bob;
bob.name = "alice";
bob.name = "bob";
printf("name: %s\n", bob.name);
// char* in main()
char *name = "kim";
*name = "lee";
printf("name: %s\n", name);
return 0;
}
Output:
name: bob
name: kim
In the case of using struct, the value of student bob.name was changed from "alice" to "bob". However, in the latter case, the value of char* name wasn't changed.
I think that the reason why "kim" didn't changed to "lee" is char *name was pointing literal "kim".
If I'm right, why bob.name was changed from "alice" to "bob"? It shoudn't have changed to "bob" because "alice" was also literal.
What's the difference?