jaybdub / enes499

Files, programs, and project management for UMD's enes499 course.
0 stars 0 forks source link

Cannot copy string of mismatching length into array #8

Closed jaybdub closed 10 years ago

jaybdub commented 10 years ago

The following does not compile


typedef struct _Simple {
    bool has_name;
    char name[20];
} Simple;

Simple msg;
msg.name = "Bob"

Error is something like :

char[3] cannot be set to char[20].

jaybdub commented 10 years ago

Instead, use strcpy of strncpy (which adds overflow protection I think?)

strcpy(msg.name,"bob");

or

strncpy(msg.name,"bob",sizeof(msg.name));