trying to store the results from reading a file that match a criteria, in this case a stop being less than 1000m from a specified latitude and longtitude. I keep getting a segmentation error. Below is the code that is causing trouble and below below is the specific part of it...
void check_stops(char *array[200][3], double home_lat, double home_lon)
{
char *location_type;
char *parent_station;
char *stop_id;
char *stop_code;
char *stop_name;
char *stop_description;
double stop_lat;
double stop_lon;
char *zone_id;
int distance;
const char delimiters[] = ",";
char* running;
int count = 0;
int col = 0;
int row = 0;
FILE *fp;
fp = fopen("stops.txt", "r");
if (fp != NULL)
{
char line[BUFSIZ];
while (fgets(line, sizeof(line), fp) != NULL)
{
if(count > 1)
{
char stringcopy[sizeof(line)];
strcpy(stringcopy, line);
running = stringcopy;
/*
The code below assumes it knows where the lat and lon are in the CSV file.
This method will not work with GTFS files from other public transport agencies
since the order of the fields will be different.
*/
separatestring(&running, delimiters); //removes location_type from stringcopy
separatestring(&running, delimiters); //removes parent_station from stringcopy
stop_id = separatestring(&running, delimiters); //gets stops_id
separatestring(&running, delimiters); //removes stop_code from stringcopy
stop_name = separatestring(&running, delimiters); //gets stop_name
separatestring(&running, delimiters); //removes stop_description from stringcopy
stop_lat = atof(separatestring(&running, delimiters)); //gets stop_lat
stop_lon = atof(separatestring(&running, delimiters)); //gets stop_lon
distance = haversine(stop_lat, stop_lon, home_lat, home_lon); //calculates distance between stop location and home location
char p = distance;
char * d = &p;
if(distance <= 1000)
{
array[row][col] = stop_id;
array[row+1][col] = stop_name;
array[row+2][col] = d;
col++;
}
}
count++;**
}
}
fclose(fp);
}
The issues arise from this block of code, whenever I remove it, it works fine (omitting the other code that goes along with this) but stops working when I put it in...
if(distance <= 1000)
{
array[row][col] = stop_id;
array[row+1][col] = stop_name;
array[row+2][col] = d;
col++;
}
}
count++;**
}
EDIT:
Fixed! Thanks guys