I'm using a function to create a dynamic array using MALLOC.
getting user input with the use of SCANF. for some reason I get this Error statement:
** Unhandled exception at 0x00007FFEE518D646 (ucrtbased.dll) in Project4STRUCTS.exe: 0xC0000005: Access violation writing location 0xFFFFFFFFCD7AE350.**
THE PROBLEM IS IN THE FUNCTION CALLED INPUT_DATA
#define _CRT_SECURE_NO_WARNINGS
#define SIZE 3
#include<stdio.h>
typedef struct student_init_data {
int ID;
int* StudentGradeArray;
int NumOfExams;
}Student_init_data;
typedef struct student_processed_data {
int ID;
int StudentAvg;
}Student_processed_data;
typedef struct statistics {
Student_processed_data* HIGH;
Student_processed_data* LOW;
int SizeofHIGH;
int SizeofLOW;
int Tavg;
}Statistics;
int* Input_Data(int Exams) {
int i;
int* arr = (int*)malloc(Exams * sizeof(int));
for (i = 0;i < Exams;i )
{
printf("enter Grade: ");
scanf("%d", &arr[i]);
}
return arr;
}
int Student_Average(int* GradeArray,int NumofExams) {
int i;
int avg=0;
for (i = 0;i < NumofExams;i )
avg = avg GradeArray[i];
avg = avg / NumofExams;
return avg;
}
int Total_Average(Student_processed_data StudentAVG[SIZE], int NumofStudents) {
int i;
int avg = 0;
for (i = 0;i < NumofStudents;i )
avg = avg StudentAVG[i].StudentAvg;
avg = avg / NumofStudents;
return avg;
}
void Classification(Student_init_data InitStudentData[SIZE],Statistics *pStats) {
int i;
Student_processed_data StudentAVG[SIZE];
pStats->SizeofHIGH = 0;
pStats->SizeofLOW = 0;
for (i = 0;i < SIZE;i )
StudentAVG[i].ID = InitStudentData[i].ID;
for (i = 0;i < SIZE;i )
{
StudentAVG[i].StudentAvg = Student_Average(InitStudentData[i].StudentGradeArray, InitStudentData[i].NumOfExams);
}
pStats->Tavg = Total_Average(StudentAVG, SIZE);
for (i = 0;i < SIZE;i )
{
if (StudentAVG[i].StudentAvg >= pStats->Tavg)
(pStats->SizeofHIGH) ;
else
(pStats->SizeofLOW) ;
}
pStats->HIGH = (Student_processed_data*)malloc((pStats->SizeofHIGH) * sizeof(Student_processed_data));
pStats->LOW = (Student_processed_data*)malloc((pStats->SizeofLOW) * sizeof(Student_processed_data));
for (i = 0;i < SIZE;i )
{
if (StudentAVG[i].StudentAvg >= pStats->Tavg)
(pStats->HIGH)[i] = StudentAVG[i];// High=pointer||same as High[i]
else
(pStats->LOW)[i] = StudentAVG[i];// HIGH={ID,AVG}, StudentAVG={ID,AVG}
}
}
void Print_Tab(Statistics Stats) {
int i;
printf("Total Average is:%d ", Stats.Tavg);
printf("\n %d number of students had AVG higher than TAVG:\n", Stats.SizeofHIGH);
for (i = 0;i < Stats.SizeofHIGH;i )
{
printf("ID:%d , AVG:%d | ", (Stats.HIGH)[i].ID, (Stats.HIGH)[i].StudentAvg);
}
printf("\n %d number of students had AVG lower than TAVG:\n", Stats.SizeofLOW);
for (i = 0;i < Stats.SizeofLOW;i )
{
printf("ID:%d , AVG:%d | ", (Stats.LOW)[i].ID, (Stats.LOW)[i].StudentAvg);
}
}
void Free_Mem(Statistics* Pfreestat, Student_init_data* PfreeStudentData) {
free(Pfreestat->LOW);
free(PfreeStudentData->StudentGradeArray);
free(Pfreestat->HIGH);
}
void main() {
int i;
Student_init_data StudentData[SIZE];
Statistics Stats;
for (i = 0;i < SIZE;i )
{
printf("\nEnter Stundent Num %d ID and Number of Exams:\n", i 1);
scanf("%d", &StudentData[i].ID);
scanf("%d", &StudentData[i].NumOfExams);
StudentData[i].StudentGradeArray = Input_Data(StudentData[i].NumOfExams);
}
Classification(StudentData ,&Stats);
Print_Tab(Stats);
Free_Mem(&Stats,&StudentData);
}
Thank you all, Eyal
I tried debugging to see the problem, and couldn't get more than the understanding that the problem falls with the SCANF.
I tried looking into this certain exception online, couldn't find something with the same issue.
CodePudding user response:
Some problems:
- You should include
#include <stdlib.h>
for the memory management functions. - Change
Free_Mem(&Stats,&StudentData);
toFree_Mem(&Stats,StudentData);
because StudentData is an array of structs. - Change
free(PfreeStudentData->StudentGradeArray);
tofor (int i = 0; i < SIZE; i ) free(PfreeStudentData[i].StudentGradeArray);
void main()
is non-standard C. Better to useint main()
and return 0 at completion.
Try with these changes.