V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
998998
V2EX  ›  C

瑟瑟发抖的萌新,我 C 语言程序哪里错了。。。signal SIGSEGV,Segmentation fault

  •  
  •   998998 · 2016-12-18 17:59:22 +08:00 · 2775 次点击
    这是一个创建于 2905 天前的主题,其中的信息可能已经有所发展或是发生改变。

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct worker { char name[30]; int ID; char position[15]; int salary1,salary2,salary3,salary4,salary5,salary6; }wo[100];
    void input(int ,int); void dis(int); void search(int);
    void del(int );
    void modify(int); void author(); void add(int ,int); void fengmian(); main() { int i; int M=0; int count=0; fengmian(); for(;;) { system("cls"); printf("\n"); printf("\t\t
    **********************************************\n"); printf("\t\t* menue \n");
    printf("\t\t
    [1] input \n"); printf("\t\t [2] display \n"); printf("\t\t [3] search \n"); printf("\t\t [4] delete \n"); printf("\t\t [5] modify \n"); printf("\t\t [6] author \n"); printf("\t\t [7] add \n"); printf("\t\t [0] exit \n"); printf("\t\t************************************************\n"); printf("\t\t\tplease select 0-7:"); scanf("%d",&i); system("cls"); switch(i) {
    case 1: input(&count,M);break; case 2: dis(count);break; case 3: search(count);break; case 4: del(&count);break; case 5: modify(count);break; case 6: author();break; case 7: add(&count,M);break; case 0: printf("\t\t\tthank you for use!\n");system("pause");exit(0);break; default: printf("\t\t\twrong!\n");system("pause");break; }
    getchar(); system("cls"); } }

    void input(int *count,int M) { int numberTemp,i,j;int ID; if(*count>=100){printf("No room exist!\n");}
    printf("\tplease input number of workers to enter:"); scanf("%d",&numberTemp); for(i=0;i<numberTemp;i++) { system("cls"); printf("The NO.%d worker\n",i+1); printf("ID:");scanf("%d",&ID); for(j=0;j<*count;j++) if(wo[j].ID==ID) {printf("There is the same ID!\n");return;} wo[i].ID=ID; printf("name:"); scanf("%s",wo[i].name); printf("position:");scanf("%s",wo[i].position); printf("jbgz:"); scanf("%d",&wo[i].salary1); printf("gwjt:"); scanf("%d",&wo[i].salary2); printf("qtbt:"); scanf("%d",&wo[i].salary3); printf("kcbf:"); scanf("%d",&wo[i].salary4); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4; (*count)++;M++; printf("\t\tsuccess\n"); getchar(); } }

    void add(int *count,int M) { int numberTemp,i,j;int ID;int p=M+1; if(*count>=100){printf("No room exist!\n");}
    printf("\tplease input number of workers to enter:"); scanf("%d",&numberTemp); for(i=p;i<p+numberTemp;i++) { system("cls"); printf("The NO.%d worker\n",i+1); printf("ID:");scanf("%d",&ID); for(j=0;j<*count;j++) if(ID==wo[j].ID) {printf("There is the same ID!\n");return;} wo[i].ID=ID; printf("name:"); scanf("%s",wo[i].name); printf("position:");scanf("%s",wo[i].position); printf("jbgz:"); scanf("%d",&wo[i].salary1); printf("gwjt:"); scanf("%d",&wo[i].salary2); printf("qtbt:"); scanf("%d",&wo[i].salary3); printf("kcbf:"); scanf("%d",&wo[i].salary4); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4; (*count)++;M++; printf("\t\tsuccess\n"); getchar(); } }

    void dis(int count) { int i; printf("\t\tThe following is all information\n"); printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); for(i=0;i<count;i++) { printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].position,wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6);
    } printf("\t\tinput enter to back\n"); getchar(); }

    void search(int count) { int n,i,flag=1;int id;char name[30]; printf("\t\t1 search by name\n"); printf("\t\t2 search by ID\n"); printf("\t\tplease select 1-2:"); scanf("%d",&n); switch(n) { case 1: printf("\t\tplease input name:"); scanf("%s",name); i=0; printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); do{ if (strcmp(name,wo[i].name)==0) { printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6); flag=0; } }while(i<count); if(flag==1) printf("\t\tnot found\n"); break; case 2: printf("\t\tplease input ID:"); scanf("%d",&id);i=0; printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); do { if (id==wo[i].ID) { printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6); flag=0; } }while(i<count); if(flag==1) printf("\t\tnot found!\n"); break; default: printf("\t\twrong!\n"); break; } printf("\t\tinput enter to back\n"); }

    void del(int *count) { int id; int i,j,flag=1; printf("\t\tplease input ID:"); scanf("%d",&id); for(i=0;i<*count&&flag;i++) {if(id==wo[i].ID) {for(j=i;j<*count-1;j++) wo[j]=wo[j+1]; flag=0; (*count)--; } else
    printf("\t\tnot found!"); }

    }

    void modify(int count) { int i,n;int id; printf("\t\tplease input the ID of worker to modify:"); scanf("%d",&id); printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); for(i=0;i<count;i++) if(id==wo[i].ID) {
    printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6); for(;;) { printf("\n"); printf("\t\t*************************************************\n"); printf("\t\t* menue \n");
    printf("\t\t
    [1] name \n"); printf("\t\t [2] position \n"); printf("\t\t [3] jbgz \n"); printf("\t\t [4] gwjt \n"); printf("\t\t [5] qtbt \n"); printf("\t\t [6] kcbf \n"); printf("\t\t [0] exit \n"); printf("\t\t************************************************\n"); printf("\t\t\tplease select 0-6:"); scanf("%d",&n); system("cls"); switch(n) {
    case 1: printf("\tnew name:");scanf("%s",wo[i].name);break; case 2: printf("\tnew position:");scanf("%s",wo[i].position);break; case 3: printf("\tnew jbgz:");scanf("%d",&wo[i].salary1); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 4: printf("\tnew gwjt:");scanf("%d",&wo[i].salary2); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 5: printf("\tnew qtbt:");scanf("%d",&wo[i].salary3); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 6: printf("\tnew kcxm:");scanf("%d",&wo[i].salary4); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 0: exit(0);break; default: printf("\t\t\twrong!\n");system("pause");break; } getchar(); system("cls"); } } }

    void author() { printf("\t\t**************************************\n\n"); printf("\t\t* \n\n"); printf("\t\t \n\n"); printf("\t\t \n\n"); printf("\t\t \n\n"); printf("\t\t \n\n"); printf("\t\t*************************************\n\n"); system("pause"); }

    void fengmian() {
    printf("\n\n\n"); printf("\t welcome to use the worker wage management system\n"); printf("\n\n\n"); printf("\t\t\tpress enter to continue\n"); getchar(); }

    14 条回复    2016-12-19 15:29:52 +08:00
    jimzhong
        1
    jimzhong  
       2016-12-18 18:16:04 +08:00   ❤️ 1
    你贴到 gist 上把。。。。
    SIGSEGV 一般是内存访问越界
    bdbai
        2
    bdbai  
       2016-12-18 18:18:53 +08:00 via Android
    用 Markdown 排下版呗

    ```c
    // 代码
    ```
    luxinxin
        3
    luxinxin  
       2016-12-18 18:38:21 +08:00 via Android   ❤️ 1
    进入调试模式跑一下看看?
    UnknownR
        4
    UnknownR  
       2016-12-18 18:42:58 +08:00   ❤️ 1
    看着蛋疼,可以排下版, v2 是支持 markdown 语法的,或者直接截 ide 的图也行
    langjiyuan
        5
    langjiyuan  
       2016-12-18 18:49:42 +08:00   ❤️ 1
    (◎﹏◎)眼花。。排一下版啊。。
    wevsty
        6
    wevsty  
       2016-12-18 19:04:03 +08:00   ❤️ 1
    首先这代码编译就应该没办法过才对
    楼主声明的函数是这样的
    void input(int, int);
    而定义又是
    void input(int *count, int M)

    int 和 int *能一样?编译器没有给 error 么?

    我不知道楼主怎么使用以后会出 SIGSEGV ,光贴个代码有啥意义?我怎么知道你做了些什么操作导致程序有什么问题?
    举个例子
    scanf("%s", wo[i].name);
    看起来没问题楼主定义的 struct 中是这样定义的
    char name[30];
    可是如果用户输入超过 30 个字符会怎么样?
    安全编码第一条,永远不要相信来自用户的输入
    chiu
        7
    chiu  
       2016-12-18 19:11:09 +08:00   ❤️ 1
    有谁这样看完了的!?
    998998
        8
    998998  
    OP
       2016-12-18 19:17:20 +08:00
    @wevsty sorry ,我在复制时候出了问题,而且。。。我在自学 C 语言,这只是一个实验的程序。。。
    zjbztianya
        9
    zjbztianya  
       2016-12-18 20:00:12 +08:00
    用 gdb 吧 神器
    owt5008137
        10
    owt5008137  
       2016-12-19 09:31:33 +08:00 via Android
    代码贴成这样,有人会看么
    bluefalconjun
        11
    bluefalconjun  
       2016-12-19 09:47:25 +08:00
    请先阅读一下 提问的智慧...
    https://lug.ustc.edu.cn/wiki/doc/smart-questions
    bombless
        12
    bombless  
       2016-12-19 11:24:45 +08:00
    http://paste.ubuntu.com/23651556/
    C:\projects>cl v.c
    Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64
    Copyright (C) Microsoft Corporation. All rights reserved.

    v.c
    v.c(199): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int'
    v.c(199): warning C4313: 'printf': '%s' in format string conflicts with argument 3 of type 'int'
    v.c(199): warning C4473: 'printf' : not enough arguments passed for format string
    v.c(199): note: placeholders and their parameters expect 9 variadic arguments, but 8 were provided
    v.c(199): note: the missing variadic argument 9 is required by format string '%d'
    v.c(215): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int'
    v.c(215): warning C4313: 'printf': '%s' in format string conflicts with argument 3 of type 'int'
    v.c(215): warning C4473: 'printf' : not enough arguments passed for format string
    v.c(215): note: placeholders and their parameters expect 9 variadic arguments, but 8 were provided
    v.c(215): note: the missing variadic argument 9 is required by format string '%d'
    v.c(259): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int'
    v.c(259): warning C4313: 'printf': '%s' in format string conflicts with argument 3 of type 'int'
    v.c(259): warning C4473: 'printf' : not enough arguments passed for format string
    v.c(259): note: placeholders and their parameters expect 9 variadic arguments, but 8 were provided
    v.c(259): note: the missing variadic argument 9 is required by format string '%d'
    Microsoft (R) Incremental Linker Version 14.00.24210.0
    Copyright (C) Microsoft Corporation. All rights reserved.

    /out:v.exe
    v.obj

    编译器输出挺有帮助的, 233
    lcsoft
        13
    lcsoft  
       2016-12-19 11:42:13 +08:00
    贴出的代码格式很乱, LZ 就打算一直让它就这样乱着了?
    loff
        14
    loff  
       2016-12-19 15:29:52 +08:00
    这代码看得我瑟瑟发抖
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2952 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:45 · PVG 20:45 · LAX 04:45 · JFK 07:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.