I try to test my GradeStat function, but it fails in main in the highlighted line: printf("%d", c[0]);
.. It gives me ERROR: segmentation fault
but I can't tell why..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int** GradeStat(int* Grades, int size_grades, int grd_range, int* count_grd, int* avg_grd) {
const int len = 100/grd_range+1;
int** collector = malloc(len * sizeof(int*));
count_grd = calloc(len, sizeof(int));
avg_grd = calloc(len, sizeof(int));
size_t i;
for(i=0; i<size_grades; i++)
count_grd[Grades[i]/grd_range]++;
for(i=0; i<len; i++) {
if (count_grd[i] == 0)
collector[i] = NULL;
else
collector[i] = malloc(count_grd[i] * sizeof(int));
}
int** temp = malloc(len * sizeof(int*));
for(i=0; i<len; i++) {
temp[i] = collector[i];
}
for(i=0; i<size_grades; i++) {
*(collector[Grades[i]/grd_range]++) = Grades[i];
avg_grd[Grades[i]/grd_range] += Grades[i]/count_grd[Grades[i]/grd_range];
}
for(i=0; i<len; i++) {
collector[i] = temp[i];
}
free(temp);
return collector;
}
int main() {
int** arr;
int grd[8] = {10, 43, 46, 49, 80, 60, 98, 100};
int* c,a;
arr = GradeStat(grd, 8, 10, c, a);
------> printf("%d", c[0]); <-------
return 0;
}