none
Stack around the variable 'date' was corrupted. RRS feed

  • 问题

  • 代码如下:

    #include <stdio.h>

    #include <Windows.h>

     

    // in 64 win7 vs2010...here    have to define a 8s' space.. so defined e00_date.wee

    struct e00_date{

    unsigned short int year;

    unsigned short int month;

    unsigned short int day;

    //unsigned char tt;

    //unsigned short week;

    };

    typedef struct e00_date e00_date;

    void main()

    {

    e00_date date;

    unsigned short int sum=0;

    puts("input a date (format: yyyy/mm/dd)");

    scanf("%d/%d/%d",&date.year,&date.month,&date.day);

    if(date.month>12||date.month<1)

    {

    puts("please input a viald value");

    system("pause");

    exit(0);

    }

     

    sum+=date.day;

    switch(date.month)

    {case 12: sum+=30;

    case 11: sum+=31;

    case 10: sum+=30;

    case 9: sum+=31;

    case 8: sum+=31;

    case 7: sum+=30;

    case 6: sum+=31;

    case 5: sum+=30;

    case 4: sum+=31;

    case 3: sum+=28;

    case 2: sum+=31;

    case 1: break;

    }

    if(((date.month%4==0)&&(date.month%100!=0))||(date.month%400==0))

    if(date.month>2)

    sum++;

    printf("\nthis the %hu days in %4hu/%02hu/%02hu(yyy/mm/dd)",sum,date.year,date.month,date.day); system("pause");

    }

    亲,scanf的时候必须要格式化scanf(%hu/%hu/%hu)输入吗?

    为啥改成

    struct e00_date{
    unsigned short int year;
    unsigned short int month;
    unsigned short int day;
    unsigned int wee;
    //unsigne char tt;
    //unsigned short week;
    };

    ...

    scanf("%d/%d/%d/%d",&date.year,&date.month,&date.day,&date.wee);

    就可以呢?

    谢谢。。

    2012年10月13日 11:26

答案

  • 你好,

    欢迎参与MSDN论坛。

    你将year、month、day定义为unsigned short int,而在输入的时候却按照整型格式(“%d”)来输入。这样的话就会有一个自动强制类型转换的过程发生。

    而unsigned short int是16位的,而int是32位的,将32位数据强制转换为16位,可能发生数据丢失,导致如标题所述的错误。

    所以将“%d”改为“%hu”就不会出错了。


    Damon Zheng [MSFT]
    MSDN Community Support | Feedback to us

    2012年10月16日 2:51
    版主