我在我的代码中遇到了一些问题,我无法修复它。任何人都可以发现 mycode 中的缺陷吗?我想从输入文件中获取城市距离并在地图中复制城市名称而不重复。我的问题是分段错误。输入文件由几行两个城市名称和它们之间的距离组成,我尝试获取城市名称并将它们复制到名为 map 的双指针。PS对不起英语不好
city1[0] = (char*)malloc(sizeof(char)*10);
city2[0] = (char*)malloc(sizeof(char)*10);
map[0] = (char*)malloc(sizeof(char)*10);
map[1] = (char*) malloc(sizeof(char)*10);
map[2] = (char*) malloc(sizeof(char));
map[2] = NULL;
fscanf(data, "%s %s %d", city1[0], city2[0], &distance);
strcpy(map[0],city1[0]);
strcpy(map[1],city2[0]);
j=2,i=1;
while(fscanf(data, "%s %s %d",city1[i],city2[i], &distance)!=EOF){
city1[i+1] = (char*)malloc(sizeof(char) *10);
city2[i+1] = (char*)malloc(sizeof(char)*10);
if(checker(map,city1[i])==1){
map = realloc(map, sizeof(char*)*(j));
map[j] = (char*) malloc(sizeof(char));
strcpy(map[j], city1[i]);
map[j+1] = (char*) malloc(sizeof(char));
map[j+1] != NULL;
j++;
}
if(checker(map,city2[i])==1){
map = realloc(map, sizeof(char*)*(j));
map[j] = (char*) malloc(sizeof(char));
map[j+1] = (char*) malloc(sizeof(char));
strcpy(map[j], city2[i]);
map[j+1] = NULL; j++;
}
}