简体   繁体   English

C ++:0xC0000005:访问冲突写入位置0x00000000

[英]C++: 0xC0000005: Access violation writing location 0x00000000

I was wondering if I can get some help on my program. 我想知道我的程序是否可以得到一些帮助。 As stated in the title, I'm getting an access violation in my program. 如标题中所述,我的程序遇到访问冲突。 I realize that it has something to do with dereferencing a garbage pointer, and I'm pretty sure I've narrowed down the line that its breaking on, I'm just not sure why it's actually breaking, and how to fix it. 我意识到这与取消引用垃圾指针有关,我很确定自己已经缩小了它的界限,我只是不确定为什么它实际上在破裂,以及如何解决它。 The code is as follows: 代码如下:

void determineInputType(char input[kMaxUserInput])
{
    char* flag = NULL;
    if (*(flag = strchr(input, '.')) != NULL)
    { 
        double grade = 0;
        if ((sscanf(input, "%lf", grade)) == 1)  // problem is on this line
        {
        //rest of the code continues here

How I'm getting the input is here: 我如何获得输入的是这里:

void getUserInput(char* userInput)
{
    printf("Enter a student's grade: ");
    fgets(userInput, kMaxUserInput, stdin);
    clearCRLF(userInput);

}

and finally the main: 最后是主要的:

int main(void)
{
    char userInput[kMaxUserInput] = { 0 };
    getUserInput(userInput);
    determineInputType(userInput);

    return 0;
}

Any help on this would be greatly appreciated as it's been stumping me for a while. 对此的任何帮助将不胜感激,因为它困扰了我一段时间。 Thanks so much for your time, if there's anything else I need to post, let me know. 非常感谢您抽出宝贵的时间,如果还有其他需要发布的信息,请告诉我。

The scanf family requires you to supply a pointer to the value you want populated: scanf系列要求您提供一个指向要填充的值的指针

if ((sscanf(input, "%lf", &grade)) == 1)  // problem is no longer on this line

The way you have it, you set grade to zero then use that as the pointer. 你有它的方式,可以设置grade为零,那么使用作为指针。

The relevant part of the standard (C99 on which C++11 defers to) is 7.19.6.2 /12 (my bold): 标准的相关部分(C ++ 11 7.19.6.2 /12 C99)为7.19.6.2 /12 (我的粗体):

a,e,f,g - Matches an optionally signed floating-point number, infinity, or NaN , whose format is the same as expected for the subject sequence of the strtod function. a,e,f,g匹配一个可选的带符号浮点数,无穷大或NaN ,其格式与strtod函数的主题序列所期望的格式相同。 The corresponding argument shall be a pointer to floating. 相应的参数应为指向浮动的指针。

l (ell) - Specifies that ... a following a , A , e , E , f , F , g , or G conversion specifier applies to an argument with type pointer to double; l (ell)-指定...后面的aAeEfFgG转换说明符适用于类型指针为double的参数 ... ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 : 0xC0000005: 访问冲突写入位置 0x00000000 C++ 与内联汇编 - : 0xC0000005: Access violation writing location 0x00000000 C++ with inline assembly 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 0xC0000005:访问冲突执行位置0x00000000 - 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 在0xC0000005中的0x6ececafa出现未处理的异常:访问冲突写入位置0x00000000 - Unhandled Exception as at 0x6ececafa in 0xC0000005 : Access violation writing location 0x00000000 IXAudio2-0xC0000005:访问冲突写入位置0x00000000 - IXAudio2 - 0xC0000005: Access violation writing location 0x00000000 复制构造函数问题 C++:“0xC0000005:访问冲突写入位置 0x00000000。” - Copy Constructor Issue C++: "0xC0000005: Access violation writing location 0x00000000." Project4.exe中0x00998876处的首次机会异常:0xC0000005:访问冲突写入位置0x00000000 - First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000 在 Project0_opengl.exe 中的 0x00000000 处抛出异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in Project0_opengl.exe: 0xC0000005: Access violation executing location 0x00000000 在 CobwebDiagram.exe 中的 0x00000000 处引发异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in CobwebDiagram.exe: 0xC0000005: Access violation executing location 0x00000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM