#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));而且沒有進行判斷,萬一內存不夠用,無法申請到內存怎麼處理。容易造成內存洩露