这是结构:
typedef struct _friend {
char *firstname;
char *lastname;
char birthdate[9];
} friend;
我对如何让用户输入一个字符串并将其作为(或)放置在friend
结构中感到困惑。另外,如果用户在我使用时输入超过 256 个字符怎么办?这是我目前所拥有的......firstname
lastname
fgets
friend *f = (friend *)malloc(sizeof(friend)); //initialize f pointer to friend
char *str;
fgets(str,256,stdin);
f->firstname = (char*)malloc(sizeof(char)*(strlen(str)+1));
strcpy(f->firstname,str);