câu hỏi noob ở đây: Tôi đang cố gắng viết một giao diện menu đơn giản, nhưng tôi vẫn gặp phải lỗi lỗi phân đoạn và tôi không thể hiểu tại sao.lỗi phân đoạn sử dụng scanf
#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;
}
tôi nhận được lỗi segmentation sau khi vào hai lĩnh vực, trong bất kỳ tùy chọn trình đơn
Bạn chưa đặt trước bất kỳ bộ nhớ nào cho chuỗi của mình. Một con trỏ uninitialized là một con trỏ uninitialized. – chris