I've looked enough about this problem on this site and still haven't found a solution. I have an array of struct, and I want to read from a file some records and store them in the structs. The problem is the allocation of the memory. This is the struct I use:
struct Rec{
    int mat;
    char *nome;
    char *cognome;
};
typedef struct Rec* Record; 
This is the readFromFile function:
void readFromFile(char* fileName, Record** data, int* pn) 
{
   char line[LINE_LENGTH];
   int n, i;
   char* token;
   printf("\n\nReading file %s\n", fileName);
   FILE* fin = fopen(fileName, "r"); 
   if (fin == NULL) 
   {  printf("Error readinf file\n");getch();
      goto end;
   }
   n = 0;         // first read to know the number of lines
   while (fgets(line, LINE_LENGTH, fin) != NULL) n++;
   n = (n < MAX_LENGTH ? n : MAX_LENGTH);
   printf("N: %d\n", n);
   *pn = n;
   //Then I allocate the memory for the n lines I previously read
   *data = (Record*)malloc(n * sizeof(Record));
   if(*data == NULL){
    printf("Problem allocating memory\n");
    exit(0);
   }
   i = 0;
   for(i = 0; i < n; i++){
        (*data)[i].nome = malloc(sizeof(char) * MAX_LENGTH + 1);
        if((*data)[i]->nome == NULL){
            printf("Problem allocating memory\n");
            exit(1);
        }
        //Here comes the problem, the allocation of the second string fails and the program exit
        (*data)[i]->cognome = malloc((sizeof(char) * MAX_LENGTH + 1));
        if((*data)[i]->cognome == NULL){
            printf("Problem allocating memory\n");
            exit(2);
        }
   }
   rewind(fin);                         
   n = 0;
   while (fgets(line, LINE_LENGTH, fin) != NULL && n < MAX_LENGTH)
   {
        token = strtok(line, ";");
        strcpy((*data)[n]->nome, token);
        token = strtok(line, ";");
        strcpy((*data)[n]->cognome, token);
        token = strtok(line, ";");
        (*data)[n]->mat = atoi(token);
        n++;
   }
   fclose(fin);                         
   end:return;
}
I've tried to modify the structure and the code in many ways but have not found a solution, I think that probably is a pointer problem but I can't figure it out. The readFromFile function was provided from the professor and was built to read int from file and I had to modify it to read records.
 
     
    