使用 scanf 的分段错误 [英] segmentation fault using scanf

查看:26
本文介绍了使用 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 
 ============
");
    printf("1. ADD
");
    printf("2. DELETE
");
    printf("3. EDIT
");
    printf("4. VERIFY
");
    printf("5. Exit
");
    printf("Selection:");
    scanf("%d", &input);
    flush();
    switch (input){

    case 1:
        printf("%s
", "Enter Name:");
        scanf("%s", name);
        flush();
        printf("%s
", "enter password" );
        scanf("%s", password);
        flush();
        printf("%s
","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:
");
        scanf("%s",name);
        flush();
        printf("Enter Password
");
        scanf("%s", password);
        flush();            
        printf("enter type:
");
        scanf("%s", type);
        flush();
        printf("enter your new username:
");
        scanf("%s",newName);
        flush();
        printf("enter your new password
");
        scanf("%s", newPassword);
        flush();
        printf("enter your new type
");
        scanf("%s",newType);
        flush();
        edit(name, password, type, newName, newPassword, newType);
        menu();
        break;
    case 4:
        printf("Enter Name
");
        scanf("%s",name);
        flush();
        printf("Enter Password
");
        scanf("%s",password);
        flush();
        verify(name, password);
        menu();
        break;
    case 5:
        return 0;
    default:
        printf("invalid input, please select from the following:
");
        menu();
}
    return 0;
    }

    int flush(){
     int ch;
     while ((ch = getchar()) != EOF && ch != '
') ;
     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.

例如,用 char name[20] 代替 char *name.(请注意,这会将您的输入限制为 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() 将写入一个 undefined内存区域.它可能在一次执行中起作用,然后在下一次执行中失败.它可能会损坏进程地址空间中其他地方的内存.

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天全站免登陆