本文共 1034 字,大约阅读时间需要 3 分钟。
#include <stdio.h> #include <stdlib.h> #include <string.h> struct namect{ char *fname; char *lname; int letters; }; void getinfo(struct namect *); void makeinfo(struct namect *); void showinfo(const struct namect *); void clearup(struct namect *); int main(void){ struct namect person; getinfo(&person); makeinfo(&person); showinfo(&person); clearup(&person); return 0; } void getinfo(struct namect *pst){ char temp[81]; printf("Please enter your first name.\n"); gets(temp); pst->fname=(char *)malloc(strlen(temp)+1); strcpy(pst->fname,temp); printf("Please enter your last name:\n"); gets(temp); pst->lname=(char *)malloc(strlen(temp)+1); strcpy(pst->lname,temp); } void makeinfo(struct namect *pst){ pst->letters=strlen(pst->fname)+strlen(pst->lname); } void showinfo(const struct namect *pst){ printf("%s %s your name contains %d letters.\n",pst->fname,pst->lname,pst->letters); } void clearup(struct namect *pst){ free(pst->fname); free(pst->lname); }
本文转sinojelly51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/410487,如需转载请自行联系原作者