【问题标题】:segmentation fault in C during scanfscanf期间C中的分段错误
【发布时间】:2014-03-24 09:10:25
【问题描述】:

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

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

【问题讨论】:

  • 谢谢你们,我现在明白了,错过了这个太愚蠢了

标签: c segmentation-fault scanf


【解决方案1】:

你错过了&在

scanf("%d",amountWindowForTop);

这一定是

scanf("%d",&amountWindowForTop);

错误原因是 & 被称为 address of operator 因此在 scanf 中缺少它意味着您将值放在哪里意味着地址是必需的,因为它指定了我们必须保留值的变量的地址. 分段错误错误通常是我们在与地址相关的任何问题时得到的。 希望对你有用。

【讨论】:

  • 你可以试试这个:www.hemingwayapp.com :)
【解决方案2】:

你错过了&amp;,

线

scanf("%d",amountWindowForTop);

应该是

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

【讨论】:

    【解决方案3】:

    你错过了&amp;

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

    【讨论】:

    • 我的天啊,我知道我需要 & 但是如果你把它放在你的脸上你不会注意到哇哇哇非常感谢
    【解决方案4】:

    你错过了&amp;

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

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多