gpt4 book ai didi

c - 尝试调用我的deleteStudent函数时出现段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 21:25:48 26 4
gpt4 key购买 nike

我尝试制作一个关于学生注册的程序。

这是我的头文件:

#ifndef STUDENTREGISTRATION_H_
#define STUDENTREGISTRATION_H_
typedef struct {
char name[30];
char surname[30];
char faculty[30];
char department[30];
int age;
int grade;
int count;
} STUDENT_STRUCT, *STUDENT;

STUDENT_STRUCT students[100];
STUDENT_STRUCT empty;
void *data_init();
void *data_set(STUDENT s);
void addStudent(int count,STUDENT s);
void displayList(int count);
void free_list(STUDENT s);
void deleteStudent(int index,int count);
void Choice();


#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "StudentREGISTRATION.h"

void *data_set(STUDENT s) {

s = students;
return s;
}
void addStudent(int count,STUDENT s) {

printf("Please enter an information about student:\n");
printf("Name:");
scanf("%s", s->name);
printf("Surname:");
scanf("%s", s->surname);
printf("Faculty:");
scanf("%s", s->faculty);
printf("Department:");
scanf("%s", s->department);
printf("Age:");
scanf("%d", &s->age);
printf("Grade:");
scanf("%d", &s->grade);
students[count].age = s->age;
students[count].grade=s->grade;
strcpy(students[count].department,s->department);
strcpy(students[count].faculty,s->faculty);
strcpy(students[count].name,s->name);
strcpy(students[count].surname, s->surname);
free_list(s);
}

void displayList(int count) {
int i;
for (i = 0; i < count; i++) {
if(students[i].name[0] != "\0" && students[i].surname[0] != "\0" && students[i].faculty[0] != "\0" && students[i].department[0] !="\0"&&students[i].age != 0 && students[i].grade != 0)
{
printf("%d.) %s %s %s %s %d %d \n", i + 1, students[i].name,
students[i].surname, students[i].faculty,
students[i].department, students[i].grade, students[i].age);
}

}
}
void free_list(STUDENT s) {

free(s);
}
void deleteStudent(int index,int count) {
int i;
for(i=0;i<count;i++)
{
if(index-1==i)
{
students[i].name[0]="\0";
students[i].surname[0]="\0";
students[i].department[0]="\0";
students[i].faculty[0]="\0";
students[i].age=0;
students[i].grade=0;
}
}
displayList(count);
}
void *data_init()
{
STUDENT s=(STUDENT)malloc(sizeof(STUDENT_STRUCT));
return s;
}
void Choice() {
STUDENT s;
int a=0;
printf("Enter your choice to do with a program\n");
printf("Press A for adding student\n");
printf("Press L to display a student list\n");
printf("Press D for deleting student\n");
printf("Press E for exit\n");
char choice = ' ';
int index;
while (choice != 'E') {

printf("Your choice:");
scanf("%c", &choice);
switch (choice) {
case 'A':
s=data_init(s);
addStudent(a,s);
a++;
break;
case 'L':

displayList(a);
break;
case 'D':
printf("Enter an index:");
scanf("%d ",index);
deleteStudent(index,a);
break;
}
}

exit(1);
}

当程序尝试在 switch case 中运行删除功能时,出现段错误(代码转储)错误。这段代码中我的错在哪里,我猜这都是关于指针的。但我尝试为每个学生分配和取消分配。问题出在哪里,如果有人告诉我,我将不胜感激。谢谢。

最佳答案

在做其他事情之前,我首先尝试检查索引是否在 0 和 count 之间。这可能会导致那里出现错误。另外,您似乎没有使用#endif 关闭头文件中的#ifndef。

也正如有人指出的那样,scanf 应该给出一个地址而不是一个变量。例如:

scanf("%d ", &z);

关于c - 尝试调用我的deleteStudent函数时出现段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26742983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com