scanf函数在分段错误使用C [英] segmentation fault in C during scanf

查看:89
本文介绍了scanf函数在分段错误使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个整数扫描使用我的程序。但是我的计划让我在编译时分段故障这是给我的错误的部分:

  INT主要(无效)
{
    INT totalHeight = 0,floorWidth = 0,amountOfStories,amountWindowForTop,amountWindowForMiddle,amountWindowForBottom,WINDOWHEIGHT,middleWindowWidth,topWindowWidth,bottomWindowWidth,minimumHeight,minimumWidth;    焦炭topFloorWindowContent,middleFloorWindowContent,bottomFloorWindowContent,windowBorder,floorBorder;    INT tempMax;    的printf(请输入您的建筑想多少故事有:);
    scanf函数(%d个,&安培; amountOfStories);
    minimumHeight = amountOfStories * 6 + 1;
    而((totalHeight&下;!minimumHeight)||((totalHeight%amountOfStories)= 1))
    {
        的printf(请输入totalHeight(最小%D):minimumHeight);
        scanf函数(%d个,&安培; totalHeight);
    }
    的printf(请输入多少个窗口建设会对顶楼:);
    scanf函数(%D,amountWindowForTop);
    的printf(请输入多少个窗口建设将有中间楼层:);

现在我的程序编译后只运行到上amoutWindowForTop scanf函数
之后我在值输入该它只是给我分割的错,我不知道为什么。因为我不使用指针,那么,为什么给我这个错误?一切似乎为了让我
这是在输出

 请输入您的建筑想多少故事有:5
请输入totalHeight(最小31):31
请输入多少个窗口建设将有顶层:2
分段故障


解决方案

您错过了&安培;

  scanf函数(%D,amountWindowForTop);

  scanf函数(%d个,&安培; amountWindowForTop);
// --------- ^

I am trying to scan in an integer to use for my program. 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. Because I am not using pointers so why is it giving me that error?everything seemed in order for me this is the 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 &,

Line

scanf("%d",amountWindowForTop);

should be

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

这篇关于scanf函数在分段错误使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆