#include
#include
struct data
{
int day,month,year;
} ;
struct stu
{
char name[20];
long num;
struct data birthday;
} ;
int main()
{
struct stu *student;
student=malloc(sizeof(struct stu));
printf("Input name,number,year,month,day:/n");
scanf("%s",student->name);
scanf("%ld",&student->num);
scanf("%d%d%d",&student->birthday.year,&student->birthday.month,
&student->birthday.day);
printf("/nOutputname,number,year,month,day/n");
printf("%20s%10ld%10d//%d//%d/n",student->name,student->num,
student->birthday.year,student->birthday.month,
student->birthday.day);
return 0;
}
程序怎么出错了,求大神解答!!
错误报告是这样的。
E:c++稿子sdas.cpp(16) : error C2440: '=' : cannot convert from 'void ' to 'struct stu *' Conversion from 'void' to pointer to non-'void' requires an explicit cast 执行 cl.exe 时出错.
首先 ,只看到了 malloc 没有看到free。student=malloc(sizeof(struct stu)); 这里有问题,应该改为 ,student=(stu*)malloc(sizeof(struct stu));而且没有进行判断,万一内存不够用,无法申请到内存怎么处理。容易造成内存泄露
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。