I am wondering why a structure pointer seems to be behave differently than a char pointer.
typedef struct person_struct {
char *name;
} person;
changeStructName(person * name1)
{
name1->name = "Robert";
}
changeCharName(char * name2)
{
name2 = "Jose";
}
int main()
{
person * name1;
char * name2;
name1 = malloc(1 * sizeof(person));
changeStructName(name1);
changeCharName(name2);
printf("First name is %s\n",name1->name);
printf("Second name is %s\n",name2);
}
I know that using this code will work:
changeCharName(char ** name2)
{
*name2 = "Jose";
}
changeCharName(&name2);
I am just curious why I do not need to do the same pointer to pointer setup to the structure for it to be modified by reference?