airPdata **airport = malloc(sizeof(airport) * (50+1));
printf("Passes airPdata **airport\n");
// buffer = malloc(sizeof(char) * (50+1));
// puts the strings into char line
while(fgets(line, 1024, fp) != NULL)
{
// has pointer value point to line
value = line;
printf("Before creating space for struct members\n");
// creating space for the struct members
airport[j]->LocID = malloc(sizeof(char)*(50+1));
airport[j]->fieldName = malloc(sizeof(char)*(50+1));
airport[j]->city = malloc(sizeof(char)*(50+1));
printf("after\n");
I'm trying to create an array of structures, but I just can't figure out how to allocate memory for the members of the struct.. It keeps segfaulting. LocID, fieldName and city are all char*
EDIT*** I figured out the problem. Using a double pointer doesn't need to allocate airport, but the members of airport still need to be allocated.
// allocates memory for the struct airPdata **airport;
// buffer = malloc(sizeof(char) * (50+1));
// puts the strings into char line
while(fgets(line, 1024, fp) != NULL)
{
// has pointer value point to line
value = line;
printf("Yes\n");
// creating space for the struct members
airport[j]->LocID = malloc(sizeof(char)*(50+1));
airport[j]->fieldName = malloc(sizeof(char)*(50+1));
airport[j]->city = malloc(sizeof(char)*(50+1));
j++;
}
However, the program seg faults when it goes back around the while loop for the second time and encounters airport[j]->LocID = malloc
airPdata *airport = malloc(sizeof(*airport) * (50+1));not**airPdata *airport[MAX]is the array of pointers, not an array of structs