stu 直接声明的指针,空间没有分配。
可考虑改为:
#include
struct date
{int year,month,day;}stu, *p_stu;
void print(struct date *stu);
void main()
{
p_stu = &stu;
scanf("%d%d%d",&p_stu->year,&p_stu->month,&p_stu->day);
print(p_stu);
}
void print(struct date *stu)
{
printf("birth is %dyear %dmonth %dday\n",stu->year,stu->month,stu->day);
}
#include
void print(struct date *stu);
struct date
{int year,month,day;} stu;//把*去掉就可以了。这样就是一个全局变量了。不是一个指针变量(内存地址的变量)。
void main()
{
scanf("%d %d %d",&stu->year,&stu->month,&stu->day);//空格隔开。区分数字格式可以用逗号。
print(&stu); //加上取地址。
}
void print(struct date *stu)
{
printf("birth is %dyear %dmonth %dday\n",stu->year,stu->month,stu->day);
}
你全局变量stu是个指针,没有malloc空间出来就赋值了scanf