因此,我有一个名为stationInfo的结构,其中包含大量信息,包括纬度,经度和站点ID。我编写了一个函数,该函数将从文件中读取数据并将值存储到结构数组中。现在,我想将这些结构数组移到另一个结构数组中。
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);
}
但是,这破坏了我的程序。我怎样才能解决这个问题?
编辑:根据帕迪的要求:
mapMarker结构:
typedef struct{
GeographicPoint location;
char markerName[100];
char markerText[1000];
int type;
} MapMarker;
GeographicPoint的位置分为纬度和经度结构。
char* getStationName(int stationID){
if (stationID < 0 || stationID >= MAX_STATIONS || !AllStationNames[stationID])
return "Unknown";
return AllStationNames[stationID];
} /* getStationName */
和数组
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",
};
最佳答案
如评论中所述,您正在声明使用变量t
作为大小的VLA(可变长度数组)。始终小于或等于MAX_STATIONS
。因此,您有一个缓冲区溢出问题。
MapMarker mapInfo[t];
int k;
for(k=0; k < MAX_STATIONS; k++){
// Accessing mapInfo[k] when k >= t will have undefined behaviour
}
最简单的解决方案是使
mapInfo
大小恒定并循环到t
:MapMarker mapInfo[MAX_STATIONS];
for( k = 0; k < t; k++ ) ...
关于c - 通过结构传递字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19920319/