简体   繁体   English

scanf 期间 C 中的分段错误

[英]segmentation fault in C during scanf

I am trying to scan in an integer to use for my program.我正在尝试扫描 integer 以用于我的程序。 However my program gives me segmentation fault during compilation this is the section that is giving me the error:但是我的程序在编译期间给了我分段错误,这是给我错误的部分:

int main(void)
{
    int totalHeight=0, floorWidth=0, amountOfStories, amountWindowForTop, amountWindowForMiddle, amountWindowForBottom, windowHeight, middleWindowWidth, topWindowWidth, bottomWindowWidth, minimumHeight, minimumWidth;

    char topFloorWindowContent, middleFloorWindowContent, bottomFloorWindowContent, windowBorder, floorBorder;

    int tempMax;

    printf("please enter how many stories your building would like to have: ");
    scanf("%d",&amountOfStories);
    minimumHeight=amountOfStories*6+1;
    while((totalHeight<minimumHeight)||((totalHeight%amountOfStories)!=1))
    {
        printf("please enter the totalHeight (minimum %d): ",minimumHeight);
        scanf("%d",&totalHeight);
    }
    printf("please enter how many window building would have for top floor: ");
    scanf("%d",amountWindowForTop);
    printf("please enter how many window building would have for middle floors: ");

now my program after compile only runs to the scanf on the amoutWindowForTop after I enter in the value for that it just gives me segmentation fault I have no idea why.现在我的程序在编译后只运行到 amoutWindowForTop 上的 scanf 在我输入值之后它只是给我分段错误我不知道为什么。 Because I am not using pointers so why is it giving me that error?everything seemed in order for me this is the output因为我没有使用指针,所以为什么会给我这个错误?一切似乎都对我来说这是 output

please enter how many stories your building would like to have: 5
please enter the totalHeight (minimum 31): 31
please enter how many window building would have for top floor: 2
Segmentation fault

You missed & in 你错过了

scanf("%d",amountWindowForTop);

this must be 这一定是

scanf("%d",&amountWindowForTop);

Cause of error is & is called address of operator so missing it in scanf means where are you put your value means address is required because it specify the address of variable where we have to keep the value. 错误的原因是&,它被称为运算符的地址,因此在scanf中缺少它意味着您将值放在哪里意味着地址是必需的,因为它指定了变量的地址,我们必须在其中保存该值。 segmentation fault error is generally we get whenever their is any problem related with address. 分段错误错误通常是每当它们与地址相关的任何问题时我们都会得到。 Hope useful for you. 希望对您有用。

You missed & in 你错过了&

 scanf("%d", amountWindowForTop);  
            ^Place & operator 

You are missing the & 您缺少&

scanf("%d",&amountWindowForTop);
           ^

You missed & , 您错过了&

Line 线

scanf("%d",amountWindowForTop);

should be 应该

scanf("%d", &amountWindowForTop);
//---------^

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

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