C语言:函数处理结构体
#include <stdio.h> #include <stdlib.h> #include <string.h> struct namect{ char fname[20]; //数组 char *lname; //指针 int letters; }; void getinfo(struct namect *); struct namect makeinfo(struct namect); void showinfo(struct namect *); int main(){ struct namect persion; getinfo(&persion); persion=makeinfo(persion); showinfo(&persion); return 0; } /** * 已指针方式传递结构体 */ void getinfo(struct namect * pst){ printf("Entenr you first name\n"); gets(pst->fname); //不读取\n,会留在输入缓冲区中呢 //fgets(pst->fname,20,stdin); printf("Entenr you last name\n"); //gets(pst->lname); char temp[100]; gets(temp); pst->lname = (char *)malloc(strlen(temp)+1); strcpy(pst->lname,temp); //fgets(pst->lname,20,stdin); } /** * 传递结构体作为参数 */ struct namect makeinfo(struct namect info){ info.letters = strlen(info.fname) + strlen(info.lname); return info; } void showinfo(struct namect * pst){ printf("%s %s,your name contains %d letters\n",pst->fname,pst->lname,pst->letters); }