c语言 结构体
#include <stdio.h>
#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];
while(n<SIZE)
{
st[n]=getinfo(st,n);
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);
temp[n].sex=(i==0)?"boy":"girl";
printf("please input the birth:\n");
scanf("%d%d",&temp[n].stu.year,&temp[n].stu.month);
}
#include <stdio.h>
#include <string.h>
#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];
while(n<SIZE)
{
st[n]=getinfo(st,n);
n++;
}
// 打印数组中的数据
for(int i = 0; i < SIZE; ++i)
{
printf("%d\t%s\t%d\t%s\t%d-%d\n", st[i].num, st[i].name, st[i].age, st[i].sex, st[i].stu.year, st[i].stu.month);
}
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);
//temp[n].sex=(i==0)?"boy":"girl"; // 改为下面一句
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);
return temp[n]; // 增加这一句
}