我遇到段错误问题。当我在带有 devc++ 或代码块的 Windows 机器上运行此特定代码时,它会按照我的预期运行。当我尝试在 linux 机器上运行时出现问题。有人告诉我,这很可能是由于指针试图访问不允许访问的位置,但我不明白为什么。
当我在没有“empinfo.txt”文件的情况下运行程序时,它将启动并允许我执行除“1-Add”之外的任何菜单选项,所以问题似乎与我的方式有关我在我的结构中使用数组。
我包含了所有代码,但唯一的问题函数(我认为)是初始化和添加。非常感谢任何有关在这种情况下使用字符数组的正确方法的帮助。
这是 empinfo.txt 文件中的示例输入
12 JackSprat 2 1 65000
13 HumptyDumpty 5 3 30000
17 BoPeep 2 3 30000
20 BoyBlue 3 2 58000
0
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *empinfo=NULL;
struct employeeData {
int EMP_ID;
char name[20];
int dept;
int rank;
double salary;
struct employeeData *next;
};
struct employeeData *head = NULL;
void initializeList();
void add(int ID, char name[0], int dept, int rank, double newSalary);
void deleteEmp(int ID);
void modify(int ID, double NewSalary);
void query(int rank);
void print();
int main(){
int choice=1, ID=0, rank=0, dept=0;
double newSalary=0;
char name[20];
initializeList();
while (choice != 0) {
printf("1-Add 2-Delete 3-Modify 4-Query 5-Print 0-Exit\n");
fflush(stdin);
scanf("%d", &choice);
if (choice == 1) {
printf("Enter new employee info. Format: \"ID name dept rank salary\"\n");
if((scanf("%d %s %d %d %lf", &ID, name, &dept, &rank, &newSalary))==5)
add(ID, name, dept, rank, newSalary);
else{
printf("invalid format entered\n\n");
continue;
}
}
else if (choice == 2){
printf("Enter employee ID to delete: ");
if(scanf("%d", &ID)==1){
deleteEmp(ID);}
else{
printf("Employee ID must be an integer\n");
continue;
}
}
else if (choice == 3){
printf("Enter employee ID to modify: ");
if(scanf("%d", &ID)==1){
printf("Enter new salary amount: ");
if(scanf("%lf", &newSalary)==1)
modify(ID,newSalary);
else
printf("Salary must be a number\n");
}
else{
printf("Employee ID must be an integer\n");
continue;
}
}
else if (choice == 4){
printf("Enter the rank you wish to query: ");
scanf("%d", &rank);
query(rank);
}
else if (choice == 5){
print();
}
}
printf("Goodbye...\n");
head=NULL;
free(head);
return 0;
}
void initializeList(){
empinfo=fopen("empinfo.txt", "r");
head = (struct employeeData *)malloc(sizeof(struct employeeData));
head->next = NULL;
if (empinfo==NULL){
printf("empinfo.txt not found, file not opened.\n");
head=NULL;
free(head);
free(empinfo);
return;
}
struct employeeData *tempPtr = head;
while (tempPtr->EMP_ID != 0){
fscanf(empinfo, "%d %s %d %d %lf", &tempPtr->EMP_ID, tempPtr->name, &tempPtr->dept, &tempPtr->rank, &tempPtr->salary);
if (tempPtr->EMP_ID == 0){
break;
}
tempPtr->next = (struct employeeData *)malloc(sizeof(struct employeeData));
tempPtr=tempPtr->next;
}
tempPtr=head;
while(tempPtr->next->EMP_ID!=0){
tempPtr=tempPtr->next;
}
empinfo=NULL;
free(empinfo);
tempPtr->next=NULL;
fclose(empinfo);
tempPtr=NULL;
free(tempPtr);
}
void add(int ID, char name[], int dept, int rank, double newSalary){
struct employeeData *tempPtr = head;
while ((tempPtr->next!=NULL) && (tempPtr->next->EMP_ID < ID)) {
tempPtr=tempPtr->next;
}
struct employeeData *newNode = (struct employeeData * )malloc(sizeof(struct employeeData));
newNode->EMP_ID = ID;
strcpy(newNode->name,name);
newNode->dept = dept;
newNode->rank = rank;
newNode->salary = newSalary;
newNode->next=NULL;
if (tempPtr==head) {
if(ID>tempPtr->EMP_ID){
newNode->next = tempPtr->next;
tempPtr->next = newNode;
}
else {
newNode->next=tempPtr;
head=newNode;
}
}
else if (tempPtr->next == NULL){
tempPtr->next=newNode;
newNode->next=NULL;
}
else{
newNode->next = tempPtr->next;
tempPtr->next = newNode;
}
printf("Employee #%d has been added to the database.\n\n", ID);
tempPtr=NULL;
newNode=NULL;
free(newNode);
free(tempPtr);
}
void deleteEmp(int ID){
struct employeeData *seek=head,*tempPtr = head;
if(head!=NULL) {
while((tempPtr->EMP_ID!=ID)){
seek = tempPtr;
tempPtr=tempPtr->next;
if(tempPtr==NULL){
printf("Employee ID not found\n");
free(tempPtr);
seek=NULL;
free(seek);
return;
}
}
if (tempPtr!=NULL){
if(tempPtr==head){
if(tempPtr->next==NULL){
printf("List cannot be empty\n");
free(tempPtr);
seek=NULL;
free(seek);
return;
}
head=tempPtr->next;
free(tempPtr);
}
else if (tempPtr->next==NULL){
free(tempPtr);
seek->next = NULL;
}
else{
seek->next=tempPtr->next;
free(tempPtr);
}
}
}
printf("Employee #%d has been deleted\n", ID);
tempPtr=NULL;
free(tempPtr);
seek=NULL;
free(seek);
}
void modify(int ID, double NewSalary){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL&&tempPtr->EMP_ID!=ID){
tempPtr=tempPtr->next;
}
if(tempPtr==NULL){
printf("Employee ID not found\n");
free(tempPtr);
return;
}
tempPtr->salary=NewSalary;
printf("Employee salary updated.\n\n");
tempPtr=NULL;
free(tempPtr);
}
void query(int rank){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL){
if(tempPtr->rank == rank){
printf("%s\n", tempPtr->name);
tempPtr=tempPtr->next;
}
else
tempPtr=tempPtr->next;
}
tempPtr=NULL;
free(tempPtr);
}
void print(){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL){
printf("%d %s %d %d %.0lf\n", tempPtr->EMP_ID, tempPtr->name, tempPtr->dept, tempPtr->rank, tempPtr->salary);
tempPtr=tempPtr->next;
}
free(tempPtr);
最佳答案
使用调试器。如果你在 Linux 上,你可能可以访问 gdb。这是您的示例和提供的测试文件的 session :
$ gdb test
...
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400bb2 in initializeList () at test.c:111
111 while(tempPtr->next->EMP_ID!=0){
Missing separate debuginfos, use: debuginfo-install glibc-2.18-14.fc20.x86_64
(gdb) p tempPtr
$1 = (struct employeeData *) 0x603250
(gdb) p tempPtr->next
$2 = (struct employeeData *) 0x0
在第111行我们可以看到我们正在尝试解引用一个NULL指针,根据这些信息应该很容易定位问题。
这也肯定会导致错误:
empinfo=fopen(...)
...
empinfo=NULL;
free(empinfo);
fclose(empinfo);
您不应该释放从 fopen
获得的句柄,也不应该将它设置为 NULL,让 fclose 处理它。
关于c - 数组的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021898/