免责声明:这是我的第一个问题在StackOverflow,我是一个新手程序员,所以我道歉,如果你是我的代码惊恐或如果我不适当地发布我的问题。(C)找不到原因核心转储错误与动态分配结构
无论如何,我正在研究一个具有动态分配结构的成绩簿。我将成绩簿分为三个结构,学生结构(学生姓名,学生ID),课程结构(课程名称,课程ID)和注册结构(学生ID,课程ID,等级)。
问题:我可以根据需要输入尽可能多的年级,而且没有第一个学生的错误。当我尝试输入第二个学生的成绩时,我的程序核心转储。我已经检查了所有变量,看看它们是否适当地传递给我的函数。以下是我的注册结构和我的添加成绩功能。
typedef struct {
int Student_ID;
int Course_ID;
int *Grade;
int GradeCount;
} Enroll_Database;
功能...
void addGrade(Enroll_Database *Enroll)
{
int i = 0, j = 0, b, Course_Num, Student_Num, Grade;
printf("Enter Course Number: ");
scanf("%d", &Course_Num);
printf("Enter Student ID: ");
scanf("%d", &Student_Num);
/* For loop that traverses through the Enroll array until until it encounters
nothing in the Course ID */
for(i = 0; Enroll[i].Course_ID != 0; i++)
{
/* if the Student Number and the Course Number are equal to their
appropriate Enroll element, then ask user to input Grade */
if(Enroll[i].Student_ID == Student_Num && Enroll[i].Course_ID == Course_Num)
{
printf("Enter Grade: ");
scanf("%d", &Grade);
if(Enroll[i].GradeCount == 0)
{
Enroll->Grade = (int *) malloc(sizeof(int));
Enroll[i].Grade[Enroll[i].GradeCount] = Grade; //core dumps
Enroll[i].GradeCount++;
}
else
{
Enroll->Grade = (int *) realloc(Enroll->Grade, sizeof(int));
Enroll[i].Grade[Enroll[i].GradeCount] = Grade; //core dumps
Enroll[i].GradeCount++;
}
}
}
}
我已经跑了多次检查和核心转储发生后,我的malloc/realloc的分配和用户输入的登记结构级别值。
我非常感谢任何帮助,如果我的代码不可读或格式错误,我会再次抱歉。 谢谢!
我已经得到它的无缝工作。谢谢!我会考虑根据您的建议调整我的代码。 – stancharoen 2014-09-25 04:14:29