这是结构体:
struct:
typedef struct _friend {
char *firstname;
char *lastname;
char birthdate[9];
} friend;
我对如何让用户输入一个字符串并将其放入friend
结构的firstname
(或lastname
)中感到困惑。此外,如果用户在使用fgets
时输入多于256个字符怎么办?目前我有以下内容...
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);
malloc()
的返回值。 - Carl Norumstr
分配内存。 - Musastr = malloc(sizeof(char)*256);
- Musa