简体   繁体   English

Visual Studio 2012 C访问代码错误

[英]Visual Studio 2012 C Access Code Error

I am working on a small homework program with C, and I encountered a very unusual issue. 我正在用C编写一个小型家庭作业程序,遇到一个非常不寻常的问题。 I was coding this in C using Visual Studio 2012. The program compiles without errors, and it also runs in cmd until a certain stage where it crashes with an exception. 我使用Visual Studio 2012在C中进行了编码。该程序可正确编译,并且也可以在cmd中运行,直到某个阶段出现异常而崩溃为止。 Please ignore the logic of the program, I cut some parts to focus on the error itself. 请忽略程序的逻辑,我削减了一些部分来专注于错误本身。 I would really appreciate some help on this. 我真的很感谢这方面的帮助。 Thank You! 谢谢!

Here is the program: 这是程序:

#include <stdio.h>
#include <math.h>

int main( void )
{
    int menuinput;
    int austinHour, austinMinute;
    int irishHour, irishMinute;
        printf("Insert a Number from 1-11 to select menu option: ");
        scanf_s("%d",&menuinput);
        switch(menuinput)
        {
            case 1:
            { 
                irishHour=0;
                irishMinute=0;
                austinHour=0;
                austinMinute =0; 
                printf("Enter Austin time: ");
                scanf_s("%d %d",austinHour,austinMinute);
                irishHour = (austinHour + 61);
                printf("%d %d",irishHour, austinMinute);
            }
                }

The error is the following when I try to run the program: 当我尝试运行程序时,错误如下:

First-chance exception at 0x62ACD745 (msvcr110d.dll) in Lab2.exe: 0xC0000005: Access violation writing location 0x00000000.

If there is a handler for this exception, the program may be safely continued.

You have an typo, You did not pass the address of arguments in scanf : 您有一个错字,您没有在scanf传递参数的地址:

 printf("Enter Austin time: ");
 scanf_s("%d %d",&austinHour,&austinMinute);
                 ^^          ^^

And I say it is an typo because you did so correctly the fist time you use scanf: 我说这是一个错字,因为您在使用scanf的第一时间就正确地做到了:

scanf_s("%d",&menuinput);

In addition to Alok Save's answer, I'm wondering if you're encountering mysterious behavioural faults from your usage of scanf. 除了Alok Save的答案之外,我想知道您是否由于使用scanf而遇到神秘的行为错误。 I have prepared some questions to consider. 我准备了一些要考虑的问题。 You can find the answers in this scanf manual , if you need a manual. 如果需要手册 ,可以在此scanf手册中找到答案。

  1. int x = scanf("%d %d", &foo, &bar); What will x be if I enter "123 hello"? 如果我输入“ 123 hello”,x将会是什么?
  2. Which word would you use to describe the value of bar? 您将使用哪个词来描述bar的价值?
  3. Which character would you expect to be returned by getchar()? 您希望getchar()返回哪个字符?
  4. do { x = scanf("%d", &bar); printf("x: %d\\n", x); } while (x == 0); What would you expect from this code, considering "hello" still remains on the stream from the last conversion failure? 考虑到上次转换失败后仍然存在“ hello”,您对这段代码会有什么期望?
  5. int y = scanf("%d", &foo); Would you expect y to be positive if scanf encountered an EOF before it could put anything into foo? 如果scanf在将任何东西放入foo中之前遇到了EOF,您是否希望y为正? EOF can be sent to stdin by pressing CTRL+Z in Windows, or CTRL+D in Linux. 可以通过在Windows中按CTRL + Z或在Linux中按CTRL + D将EOF发送到stdin。
  6. int z = scanf("%d %d", &foo, &bar); What would you expect z to be, assuming scanf successfully assigned values to the two variables foo and bar? 假设scanf成功为两个变量foo和bar赋值,您期望z是什么?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM