使用整数scanf函数分段故障 [英] segmentation fault using scanf with integer

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

问题描述

我在我的C code试图读取用户与以下functon整数输入时得到一个分段错误:

  INT userChoice = 0,TS;
浮TR,TW,TP,AP;
炭标题[35],TITLE2 [35];
书* CURR;
而(userChoice!= 9){
    的printf(1确定并打印总收入\\ n);
    的printf(2确定并打印总成本批发\\ n);
    的printf(3确定并打印利润总额\\ n);
    的printf(4确定并打印销售的\\ n总数。);
    输出(5确定并打印每销售\\ n平均利润。);
    的printf(6打印簿列表\\ n);
    的printf(7添加书\\ n);
    的printf(8删除书\\ n);
    的printf(9退出程序\\ n);
    scanf函数(%D,userChoice);
    ...
    ...

我执行我的程序时,它都会让我进入分配给userChoice的数量,但经过赛格故障立即生效。任何帮助吗?谢谢!

编辑:确切的错误我收到我:

 程序接收到的信号SIGSEFV,Segmentaion故障。
从/lib64/libc.so.6 0x0000003503256f50在_IO_vfscanf_i​​nternal()


解决方案

应该是:

  scanf函数(%d个,&安培; userChoice);

极品符号读取到userChoice的位置,而不是userChoice的价值。

I am getting a segmentation fault in my C code when trying to read integer input from the user with the following functon:

int userChoice = 0, tS;
float tR, tW, tP, aP;
char title[35], title2[35];
Book *curr;
while (userChoice != 9) {
    printf("1. Determine and print total revenue\n");
    printf("2. Determine and print total wholesale cost\n");
    printf("3. Determine and print total profit\n");
    printf("4. Determine and print total number of sales\n");
    printf("5. Determine and print average profit per sale\n");
    printf("6. Print book list\n");
    printf("7. Add book\n");
    printf("8. Delete book\n");
    printf("9. Exit the program\n");
    scanf("%d", userChoice);
    ...
    ...

Every time I execute my program, it allows me to enter the number to be assigned to userChoice, but seg faults immediately after. Any help? Thanks!

Edit: The exact error I'm getting i:

Program received signal SIGSEFV, Segmentaion fault. 
0x0000003503256f50 in _IO_vfscanf_internal () from /lib64/libc.so.6

解决方案

Should be:

scanf("%d", &userChoice);

Need the ampersand to read it into the location of userChoice, and not the value of userChoice.

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

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