|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
void print(int k,struct information y);
struct time
{
int year;
int month;
int day;
};
struct information
{
char name[20];
char sex;
int age;
struct time birthday;
}student[3];
main()
{
struct information* p;
p = student;
int i;
for (i = 1; i < 4; i++)
{
printf("input %d name:", i);
scanf_s("%s", &p->name, 20);
getchar(1);
printf("input %d sex:", i);
scanf_s("%c", &p->sex, 1);
getchar(1);
printf("input %d age:", i);
scanf_s("%d", &p->age);
printf("input %d birthday year:", i);
scanf_s("%d", &p->birthday.year);
printf("input %d birthday month:", i);
scanf_s("%d", &p->birthday.month);
printf("input %d birthday day:", i);
scanf_s("%d", &p->birthday.year);
p++; //指针P加一跨度为结构体变量数组下一元素 student[0],student[1],student[2]
}
print(1, student[0]);
print(2, student[1]);
print(3, student[2]);
}
void print(int k, struct information y)
{
printf("student%d information:\n", k);
printf("name is:%s\n", y.name);
printf("sex is:%c\n", y.sex);
printf("age is:%d\n", y.age);
printf("birthday is:%d-%d-%d\n", y.birthday.year, y.birthday.month, y.birthday.day);
}
程序报错调用函数时形参与实参类型不一致,但程序可运行。
运行结果:
input 1 name:yangke
input 1 sex:w
input 1 age:13
input 1 birthday year:1999
input 1 birthday month:3
input 1 birthday day:25
input 2 name:zouyifan
input 2 sex:m
input 2 age:18
input 2 birthday year:1998
input 2 birthday month:6
input 2 birthday day:12
input 3 name:zhengquan
input 3 sex:m
input 3 age:15
input 3 birthday year:1997
input 3 birthday month:19
input 3 birthday day:5
student1 information:
name is:yangke
sex is:w
age is:13
birthday is:25-3-0
student2 information:
name is:zouyifan
sex is:m
age is:18
birthday is:12-6-0
student3 information:
name is:zhengquan
sex is:m
age is:15
birthday is:5-19-0
根据运行结果看,除了出生年与日,其余数据均能正常输入输出,求大神破解,谢谢。
这里错了
printf("input %d birthday day:", i);
scanf_s("%d", &p->birthday.year);
应该是
scanf_s("%d", &p->birthday.day);
|
|