c - scanf期间C中的段错误

标签 c segmentation-fault scanf

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

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

最佳答案

你错过了&在

scanf("%d",amountWindowForTop);

这一定是

scanf("%d",&amountWindowForTop);

错误原因是 & 被称为运算符地址,因此在 scanf 中缺少它意味着您将值放在哪里意味着地址是必需的,因为它指定了我们必须在其中保存值的变量的地址。 每当出现任何与地址相关的问题时,我们通常都会遇到段错误错误。 希望对您有用。

关于c - scanf期间C中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605044/

相关文章:

c - 如何给指针起别名

c - 在用 C 语言构建决策树时如何存储决策树?

Linux:在 CentOS v7 上加载调试符号

c - 在简单的 GTK+ 应用程序中启用优化时出现段错误?

c - libsigsegv 和响应堆栈溢出

c++ - 为什么 scanf 似乎跳过了输入?

c - fread/ftell 显然在 Windows 下损坏,在 Linux 下工作正常

c++ - 在 C 中清洁屏幕

Char* 在不应该的时候显示 null

c - c语言新手,我的代码有什么问题