为什么memcpy(&calendar, cal, sizeof(CALENDAR))执行后就有数据错误呢,求原因
#include<iostream>using namespace std;
typedef struct
{
int year;
int month;
int date;
int hour;
int minute;
int second;
int millisecond;
}CALENDAR;
//CALENDAR* getCalendar()
//{
// CALENDAR *cal = (CALENDAR *)malloc(sizeof(CALENDAR));
// cal->year = 2015;
// cal->month = 8;
// cal->date = 15;
// cal->hour = 14;
// cal->minute = 34;
// cal->second = 23;
// cal->millisecond = 123;
// return cal;
//}
CALENDAR *getCalendar()
{
CALENDAR cal ;
cal.year = 2015;
cal.month = 8;
cal.date = 15;
cal.hour = 14;
cal.minute = 34;
cal.second = 23;
cal.millisecond = 123;
return &cal;
}
int main()
{
CALENDAR calendar;
CALENDAR* cal;
cal = getCalendar();
#if 0
calendar.year = cal->year;
calendar.month = cal->month;
calendar.date = cal->date;
calendar.hour = cal->hour;
calendar.minute = cal->minute;
calendar.second = cal->second;
calendar.millisecond = cal->millisecond;
#endif
memcpy(&calendar, cal, sizeof(CALENDAR));
cout << calendar.year << " "
<< calendar.month << " "
<< calendar.date << " "
<< calendar.hour << " "
<< calendar.minute << " "
<< calendar.second << " "
<< calendar.millisecond << " "
<< sizeof(CALENDAR) << endl;
system("pause");
return 0;
}
[ 本帖最后由 lianjiecuowu 于 2015-8-16 13:42 编辑 ]