简体   繁体   English

使用scanf进行分段错误

[英]segmentation fault using scanf

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. noob问题:我正在尝试编写一个简单的菜单界面,但我不断收到分段错误,我无法弄清楚原因。

#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 在任何菜单选项中输入两个字段后,我得到分段错误

You need to initialize your pointers. 你需要初始化你的指针。 Alternatively, use stack-allocated arrays. 或者,使用堆栈分配的数组。

For example, instead of char *name , do char name[20] . 例如,不要使用char *name ,而是使用char name[20] (Note that this will limit your input to 19 characters; use a larger buffer if necessary.) (请注意,这会将输入限制为19个字符;如有必要,请使用更大的缓冲区。)

Right now, you are passing uninitialized pointers into scanf() which effectively means that scanf() is going to write to an undefined area of memory. 现在,您将未初始化的指针传递给scanf() ,这实际上意味着scanf()将写入未定义的内存区域。 It might work on one execution and then fail on the next. 它可能在一次执行时起作用,然后在下一次执行时失败。 It might corrupt memory elsewhere in the process' address space. 它可能会破坏进程地址空间中其他位置的内存。

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. 编译器可以捕获这样的错误并发出警告。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM