C语言 使用 scanf 出现分段错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13447891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:28:34  来源:igfitidea点击:

segmentation fault using scanf

csegmentation-fault

提问by agarrow

noob question here: I'm trying to write a simple menu interface, but I keep getting a segmentation fault error and I can't figure out why.

菜鸟问题:我正在尝试编写一个简单的菜单界面,但我不断收到分段错误错误,我不知道为什么。

#include <stdlib.h>
#include <stdio.h>
int flush(); int add(char *name, char *password, char *type); int delete(char *name);
int edit(char *name, char *password, char *type, char *newName, char *newPassword, char            *newType);
int verify(char *name, char *password);



int menu(){
    int input;
    char *name, *password, *type, *newName, *newPassword, *newType;
    printf("MAIN MENU \n ============\n");
    printf("1. ADD\n");
    printf("2. DELETE\n");
    printf("3. EDIT\n");
    printf("4. VERIFY\n");
    printf("5. Exit\n");
    printf("Selection:");
    scanf("%d", &input);
    flush();
    switch (input){

    case 1:
        printf("%s\n", "Enter Name:");
        scanf("%s", name);
        flush();
        printf("%s\n", "enter password" );
        scanf("%s", password);
        flush();
        printf("%s\n","enter type" );
        scanf("%s",type);
        add(name, password, type);
        menu();
        break;
    case 2:
        printf("Enter Name:" );
        scanf("%s",name);
        flush();
        delete(name);
        menu();
        break;
    case 3:
        printf("Enter Name:\n");
        scanf("%s",name);
        flush();
        printf("Enter Password\n");
        scanf("%s", password);
        flush();            
        printf("enter type:\n");
        scanf("%s", type);
        flush();
        printf("enter your new username:\n");
        scanf("%s",newName);
        flush();
        printf("enter your new password\n");
        scanf("%s", newPassword);
        flush();
        printf("enter your new type\n");
        scanf("%s",newType);
        flush();
        edit(name, password, type, newName, newPassword, newType);
        menu();
        break;
    case 4:
        printf("Enter Name\n");
        scanf("%s",name);
        flush();
        printf("Enter Password\n");
        scanf("%s",password);
        flush();
        verify(name, password);
        menu();
        break;
    case 5:
        return 0;
    default:
        printf("invalid input, please select from the following:\n");
        menu();
}
    return 0;
    }

    int flush(){
     int ch;
     while ((ch = getchar()) != EOF && ch != '\n') ;
     return 0;
    }

I get the segmentation fault after entering two fields, in any menu option

在任何菜单选项中输入两个字段后出现分段错误

回答by cdhowie

You need to initialize your pointers. Alternatively, use stack-allocated arrays.

您需要初始化您的指针。或者,使用堆栈分配的数组。

For example, instead of char *name, do char name[20]. (Note that this will limit your input to 19 characters; use a larger buffer if necessary.)

例如,代替char *name,执行char name[20]。(请注意,这会将您的输入限制为 19 个字符;如有必要,请使用更大的缓冲区。)

Right now, you are passing uninitialized pointers into scanf()which effectively means that scanf()is going to write to an undefinedarea of memory. It might work on one execution and then fail on the next. It might corrupt memory elsewhere in the process' address space.

现在,您正在传递未初始化的指针scanf(),这实际上意味着scanf()将写入未定义的内存区域。它可能在一次执行中工作,然后在下一次执行中失败。它可能会破坏进程地址空间中其他地方的内存。

Don't use uninitialized variables, and consider turning up your compiler warnings as high as they will go; the compiler can catch errors like this and emit a warning.

不要使用未初始化的变量,并考虑尽可能提高编译器警告;编译器可以捕获这样的错误并发出警告。

回答by Viswesn

Instead of using *name, *password,.. use name[100], password[100],... If you want name, password, .. to be pointer then allocate memory using malloc or calloc before calling scanf.

而不是使用 *name, *password,.. 使用 name[100], password[100],... 如果你想要 name, password, .. 作为指针,那么在调用 scanf 之前使用 malloc 或 calloc 分配内存。