c语言结构体函数调用参数怎么设置
函数结构是下面的代码,main函数中如何调用showinfo函数,参数应该怎么设置,对参数的设置不太明白
C/C++ code#include
#define SIZE 5
#define LEN 40
struct 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
{
st[n]=getinfo(st,n);
n++;
}
for(n=0;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
#define SIZE 2
#define LEN 40
struct 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
{
//st[n]=getinfo(st,n);
getinfo(st,n);
n++;
}
for(n=0;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);
}