读书人

c语言结构体函数调用参数如何设置

发布时间: 2012-08-03 00:12:14 作者: rapoo

c语言结构体函数调用参数怎么设置
函数结构是下面的代码,main函数中如何调用showinfo函数,参数应该怎么设置,对参数的设置不太明白

C/C++ code
#include <stdio.h>#define SIZE 5#define LEN 40struct birth{int year;int month;};struct student{int num;char name[LEN];int age;char sex[LEN];struct birth stu;};struct student getinfo(struct student *,int);struct student showinfo(struct student);int main(void){int n=0;int i;struct student st[SIZE];struct student *pst=st;while(n<SIZE){st[n]=getinfo(st,n);n++;}for(n=0;n<SIZE;n++){showinfo(pst,n);}return 0;}struct student getinfo(struct student *temp,int n){int i;printf("NO is %d\n",n+1);temp[n].num=n+1;printf("please input the name:\n");scanf("%s",temp[n].name);printf("please input the age:\n");scanf("%d",&temp[n].age);printf("please enter sex(0 is boy, 1 is girl)");scanf("%d",&i);strcpy(temp[n].sex,(i==0)?"boy":"girl");printf("please input the birth:\n");scanf("%d%d",&temp[n].stu.year,&temp[n].stu.month);}struct student showinfo(struct student *pst,int n){printf("%d %s %d %s %d-%d \n",(pst+n)->num,(pst+n)->name,(pst+n)->age,(pst+n)->sex,(pst+n)->stu.year,(pst+n)->stu.month);}


[解决办法]
struct student showinfo(struct student, int n);//声明的时候差个参数,第一个参数差个指针。其实返回值用空就行了。建议写成void showinfo(struct student *temp, int n);//实现函数部分也要对应修改。main里面调用showinfo没有问题。
[解决办法]
C/C++ code
#include <stdio.h>#define SIZE 2#define LEN 40struct birth{    int year;    int month;};struct student{    int num;    char name[LEN];    int age;    char sex[LEN];    struct birth stu;};void getinfo(struct student *,int);//void showinfo(struct student *);//int main(void){    int n=0;    int i;    struct student st[SIZE];    struct student *pst=st;    while(n<SIZE)    {        //st[n]=getinfo(st,n);        getinfo(st,n);        n++;    }    for(n=0;n<SIZE;n++)    {        showinfo(pst,n);    }    return 0;}void getinfo(struct student *temp,int n)//{    int i;    printf("NO is %d\n",n+1);    temp[n].num=n+1;    printf("please input the name:\n");    scanf("%s",temp[n].name);    printf("please input the age:\n");    scanf("%d",&temp[n].age);    printf("please enter sex(0 is boy, 1 is girl)");    scanf("%d",&i);    strcpy(temp[n].sex,(i==0)?"boy":"girl");    printf("please input the birth:\n");    scanf("%d%d",&temp[n].stu.year,&temp[n].stu.month);}void showinfo(struct student *pst,int n)//{    printf("%d %s %d %s %d-%d \n",(pst+n)->num,(pst+n)->name,(pst+n)->age,(pst+n)->sex,(pst+n)->stu.year,(pst+n)->stu.month);} 

读书人网 >C语言

热点推荐