失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > c语言结构体调用成员函数 c语言结构体函数调用参数如何设置

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

时间:2022-07-15 05:09:56

相关推荐

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

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);

}

如果觉得《c语言结构体调用成员函数 c语言结构体函数调用参数如何设置》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。