Code:
while(fgets(line, sizeof(line), pdbfile)) // Loop to find the number of atoms
{
if (line[0]== 'A' && line[1] == 'T' && line[2] == 'O' && line[3] == 'M')
{
i++;
}
}
Atom *arr;
arr = malloc(i*(sizeof(Atom)));
fseek(pdbfile, 0, SEEK_SET); // Seeking the cursor back to start of file
k = 0;
while(fgets(line, sizeof(line), pdbfile))
{
if (line[0]== 'A' && line[1] == 'T' && line[2] == 'O' && line[3] == 'M')
{
p = line;
i = 0;
j = 0;
flag = 0;
strcpy(arr[k].type,"ATOM");
copyField(temp[1],line,7,11);
arr[k].an = strtol(temp[1],NULL,10);
copyField(temp[2],line,13,16);
strcpy(arr[k].mol,temp[2]);
copyField(temp[3],line,18,20);
strcpy(arr[k].amino,temp[3]);
copyField(temp[4],line,22,22);
strcpy(arr[k].chainid,temp[4]);
copyField(temp[5],line,23,26);
arr[k].pos=strtol(temp[5],NULL,10);
copyField(temp[6],line,31,38);
arr[k].xc=strtof(temp[6],&endptr);
copyField(temp[7],line,39,46);
arr[k].yc=strtof(temp[7],&endptr);
copyField(temp[8],line,47,54);
arr[k].zc=strtof(temp[8],&endptr);
copyField(temp[9],line,55,60);
arr[k].occ=strtof(temp[9],&endptr);
copyField(temp[10],line,61,66);
arr[k].temp=strtof(temp[10],&endptr);
copyField(temp[11],line,77,78);
strcpy(arr[k].element,temp[11]);
//printf("\n%4d %5s %5s %5s %4d %7.3f %7.3f %7.3f %7.3f %7.3f ",arr[k].an, arr[k].mol, arr[k].amino, arr[k].chainid, arr[k].pos, arr[k].xc, arr[k].yc, arr[k].zc,arr[k].occ,arr[k].temp,arr[k].element);
k++;
}
}
return arr;
}
However, when i tried checking other warnings and clear all the unallocated variables in this big problem 6412 lines, I found the same dynamic memory allocation (Segmentation error) and the warning that pow, sqrt, atan, cos, sin functions are not defined although i have included math.h.