标题:为什么memcpy(&calendar, cal, sizeof(CALENDAR))执行后就有数据错误呢,求 ...
只看楼主
lianjiecuowu
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:152
专家分:107
注 册:2011-5-20
结帖率:85.71%
已结贴  问题点数:20 回复次数:2 
为什么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 编辑 ]
搜索更多相关主题的帖子: return include calendar minute second 
2015-08-16 13:08
corfox
Rank: 2
等 级:论坛游民
帖 子:15
专家分:53
注 册:2015-7-29
得分:10 
memcpy是头文件<string.h>中的函数,详情请查看C或C++帮助手册。话说把C++当C用,怎么看怎么不爽啊!
2015-08-16 14:53
实际应用
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:89
专家分:341
注 册:2015-5-30
得分:10 
cal 是 getCalender 函数里的局部变量,
不能在main 里使用
2015-08-16 20:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-456428-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.376940 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved