使用带整数的scanf进行分段错误

时间:2014-10-08 00:21:42

标签: c gcc

当我尝试使用以下功能从用户读取整数输入时,我的C代码中出现了分段错误:

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);
    ...
    ...

每次执行我的程序时,都允许我输入要分配给userChoice的数字,但之后会立即出现seg错误。有帮助吗?谢谢!

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

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

2 个答案:

答案 0 :(得分:6)

应该是:

scanf("%d", &userChoice);

需要&符号将其读入userChoice的位置,而不是userChoice的值。

答案 1 :(得分:0)

scanf("%d", userChoice);

=>

scanf("%d", &userChoice);
相关问题