访问冲突错误C.

前端之家收集整理的这篇文章主要介绍了访问冲突错误C.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是初学者,我正在学习C和C.我试图在Visual Studio 2012 Express for Windows桌面中运行此代码.这是一个简单的计算器代码,我自己写的!但每当我运行它时,我得到此错误在Calculator.exe中0x519600B4(msvcr110d.dll)的未处理异常:0xC0000005:访问冲突写入位置0x00000000.

请原谅我的任何错误(这是我的第一次).谢谢!

#include<stdio.h>
#include<conio.h>

main ()
{
    int num1,num2,result;
    char oper;
    scanf_s("%d%c%d",&num1,&oper,&num2);
    switch(oper)
    {
    case '+':
        result = num1 + num2;
        printf("%d",result);
        break;
    case '-':
        result = num1 - num2;
        printf("%d",result);
        break;
    case '*':
        result = num1 * num2;
        printf("%d",result);
        break;
    case '/':
        result = num1 / num2;
        printf("%d",result);
        break;
    default:
        printf("ERROR: INVALID OR UNRECOGNISED INPUT\n");
        break;
    }
    _getch();
}

解决方法

使用 scanf_s时,对于%c格式字符串,您必须指定要读取的字符数:
scanf_s("%d%c%d",1,&num2);

documentation描述了要求:

Unlike scanf and wscanf,scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c,C,s,S,or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

猜你在找的C&C++相关文章