So I've got a struct called stationInfo, which has a bunch of info, including latitude, longitude, and station ID. I have written a function that will puts reads from a file and stores the values into arrays of structs. Now, I want to move those arrays of structs into another array of structs.
MapMarker mapInfo[t];
int k;
for(k=0; k < MAX_STATIONS; k++){
mapInfo[k].location.latitude = stationInfo[k].location.latitude;
mapInfo[k].location.longitude = stationInfo[k].location.longitude;
char* stationName = getStationName(stationInfo[k].stationID);
strcpy(mapInfo[k].markerName, stationName);
}
However, this is breaking my program. How can I fix this?
EDIT: As per Paddy's request:
mapMarker Struct:
typedef struct{
GeographicPoint location;
char markerName[100];
char markerText[1000];
int type;
} MapMarker;
GeographicPoint location is split into a latitude and logitude struct.
char* getStationName(int stationID){
if (stationID < 0 || stationID >= MAX_STATIONS || !AllStationNames[stationID])
return "Unknown";
return AllStationNames[stationID];
} /* getStationName */
And the Array
char *AllStationNames[MAX_STATIONS] = {
[1] = "Ian Stewart Complex/Mt. Douglas High School",
[3] = "Strawberry Vale Elementary School",
...
[197] = "RASC Victoria Centre",
[199] = "Trial Island Lightstation",
[200] = "Longacre",
};
MapMarkerstruct definition. Also, you should showgetStationName.tyou are using for the VLA declaration ofmapInfo? Surely you meant to useMAX_STATIONS.tcan be less thanMAX_STATIONS, you will have a buffer overrun. I don't see anything wrong with thestrcpy. Usingstrncpyis safer, but I think the problem is mismatching the array size with the number of elements you are writing to. Of course, you could just loop totinstead ofMAX_STATIONS.