首页 文章

扫描期间C中的分段错误

提问于
浏览
-1

我试图扫描一个整数用于我的程序 . 但是我的程序在编译期间给了我分段错误,这是给我错误的部分:

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: ");

现在我编译后的程序只运行到amoutWindowForTop上的scanf后输入值,它只是给我分段错误我不明白为什么 . 因为我没有使用指针所以为什么它会给我这个错误呢?一切似乎都是为了我这是输出

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

4 回答

  • 1

    你错过了 &

    线

    scanf("%d",amountWindowForTop);
    

    应该

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

    你错过了

    scanf("%d",amountWindowForTop);
    

    这一定是

    scanf("%d",&amountWindowForTop);
    

    错误的原因是 & 被称为 address of operator 所以在scanf中丢失它意味着你把你的值放在哪里意味着地址是必需的,因为它指定了我们必须保留值的变量的地址 . 分段错误错误通常是我们每当遇到与地址有关的任何问题时得到的 . 希望对你有用 .

  • 2

    你错过了 &

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

    你错过了 &

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

相关问题