1. 先看个例子
#include <stdio.h> #include <string.h> #define MAXTITL 41 #define MAXAUTL 31 struct book { /*结构模板,标记是 book */ char title[MAXTITL]; char author[MAXAUTL]; float value; }; char * s_gets(char *, int); int main() { struct book library; /* 把 library 声明为一个 book 类型的变量*/ printf("请输入书的标题: "); s_gets(library.title, MAXTITL); printf("现在输入书的作者姓名: "); s_gets(library.author, MAXAUTL); printf("现在输入书本的价格: "); scanf("%f", &library.value); printf("%s by %s: $%.2f ", library.title, library.author, library.value); printf("%s: "%s"($%.2f) ", library.author, library.title, library.value); printf("Done. "); return 0; } char * s_gets(char *st, int n) { char * ret_val; char * find; ret_val = fgets(st, n, stdin); if (ret_val) { find = strchr(st, ' '); //查找换行符 if (find) //如果地址不是NULL *find = '