I'm trying to write to a pointer by accessing it and allocating memory from other function. But after a few iterations on the while statement, core dumped is thrown. I get the data from a file that it's ensured to be in the correct format.
It fails when there are still names to read from the file (checked with the debugger)
Main
Category *category = NULL;
memoryAllocate(&category);
Memory Allocation
memoryAllocate(Category **category){
int elements;
char name[NAME];
int i = 0;
FILE *fPeces;
[...]
//Get the number of elements from the file
fscanf(fPeces, "%d", &elements);
*category = (Category *)malloc(sizeof(Category) * elements));
if(*category == NULL){
//ERROR
}
else{
while(i < elements){
fgets(name, NAME, fPeces);
strcpy(category[i]->categoryName, name);
i++;
}
}
Structure
typedef struct categoria {
char categoryName[NAME];
int part_num;
Parts *parts;
} Category;
FILE (first number is the number of categories and the rest are the names)
6
category 1
category 2
category 3
category 4
category 5
category 6