I have the following code:
typedef struct{
char *name;
int age;
} person;
int main(){
person Peter = {"peter", 19};
person Petercp = Peter;
Peter.name[0] = 'a';
Petercp.name = "hello";
printf("%s %d\n", Peter.name, Peter.age);
printf("%s %d\n", Petercp.name, Petercp.age);
}
the compiler gives me the error message of "BAD ACCESS" for the line
Peter.name[0] = 'a'
but the following line seems good
Petercp.name = "hello";
It seems as if the array of person.name is a pointer to constant. Am I right to make the conclusion?
And, if I declare the array inside the struct to be
char name[];
I am again allowed to make the change for
Peter.name[0] = 'a'
Why is that?