I have a misunderstanding regarding this code –
typedef struct _EXP{
int x;
char* name;
char lastName[40];
}XMP
...main...
XMP a;
a.name = "eaaa";
a.lastName = strcpy(a.lastName, "bbb");
Why can’t I use: a.lastName = "bbbb"; and that’s all?
Well consider the types here. The array has the contents of the string, while the
char*merely points to the data. Consequently the array requiresstrcpyand friends.Besides, if you allocated memory for the
char*on the heap or stack and then wanted to assign some content to that, you’d also have to usestrcpybecause a mere assignment would create a dangling pointer (i.e. a memory leak).