This question already has answers here:
Program crashes with 0xC0000005
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
#include<stdio.h>

int main()
{
    struct MyBookPrice//tag
    {
        int bookid; //struct member1
        int price;      //struct member2
        char pubdate;   //struct member3

    }bookstruct;

    //assign value to the struct..
    bookstruct.bookid;
    bookstruct.price;
    bookstruct.pubdate;

    printf("\nGive new value to book ID: ");
    scanf("%d",&bookstruct.bookid);

    printf("\nGive new value to book Price: ");
    scanf("%d",&bookstruct.price);

    printf("\nGive new value of book Published Date: ");
    scanf("%s",&bookstruct.pubdate);//should be like 20/12/2017

    //accessing and display struct
    printf("\n New Labell =  %d",bookstruct.bookid);
    printf("\n New Label2 =  RM%d",bookstruct.price);
    printf("\n New Label3 =  %s",bookstruct.pubdate);

    system("pause");
    return 0;
}


嗨,大家好,从上面的代码开始..我可以执行程序...并且我可以成功输入bookstruct.bookid,bookstruct.price和book.pubdate的scanf值...但是当它向下显示时,数据返回...它仅向我显示bookstruct.bookid和bookstruct.price的值,并且..最终崩溃了..但未显示bookstruct.pubdate的值。

即时通讯之间使用dev-C ++ 5.11 ..你们可以帮我吗...我在某个地方错了..thx提前..

最佳答案

pubdate是单个char,因此不能读取超过1个字符。

因此,您的代码的行为是不确定的。

一种替代方法是对该成员使用带有一定数量元素的char[]。见Char arrays and scanf function in C

10-08 03:55