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

查看:147
本文介绍了使用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.

#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.

例如,而不是的字符*名称,DO 字符名称[20] 。 (请注意,这将你的输入限制为19个字符;如果需要使用更大的缓冲区)

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

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

Right now, you are passing uninitialized pointers into scanf() which effectively means that scanf() is going to write to an undefined area of memory. 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.

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

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