【发布时间】:2022-01-16 09:29:26
【问题描述】:
您好,我刚刚完成了我的项目的编码,但是当我尝试编译时,我一直收到此错误。哪个是'<=': 'int' differs in levels of indirection from 'int *',我不知道发生了什么,我已经检查了指针并在定义变量类型时已经将其放入,但仍然无法安全编译。
#define _CRT_SECURE_NO_WARNINGS
/* C Program to find Shortest Distances or Path */
#include<stdio.h>
#define WORD 200
int main(void) {
int path, point = 1;
int* ncity[WORD];
char* cityname[WORD][WORD];
float distance[WORD][WORD];
float total[WORD];
printf("This program will input 5 path and calculates the minimum between KL Sentral, KL & Jurong East, Singapore");
printf("\n\t\t-------------------------------------------------------------");
printf("\n\t\tMinimum Path between KL Sentral, KL & Jurong East, Singapore");
printf("\n\t\t-------------------------------------------------------------");
printf("\nPlease enter the total path that you want to calculate: ");
scanf("%d", &path);
for (int i = 1; i <= path; i++) {
printf("\n\n----Path %d----", i);
printf("\nState the number of city that the path cross: ");
scanf("%d", ncity[i]);
for (int x = 1; x <= path; x++) {
for (int y = 1; y <= ncity[i]; y++) {
printf("\nCity %d named : ", y);
scanf("%s", &cityname[x][y]);
printf("\nEnter the distance to the city %d: ", y);
scanf("%f", &distance[x][y]);
total[x] = +distance[x][y];
}
}
}
//Find the minimum path
for (int x = 1; x <= path; x++) {
if (total[x] < total[point]) {
point = x;
}
}
printf("\nThe minimum path between KL Sentral, Kuala Lumpur & Jurong East, Singapore");
printf("\nPath: Path %d", point);
printf("\nTotal Distance: %f", total[point]);
printf("\n\t\tCity Name");
//Loop for 42
for (int z = 1; z <= ncity[point]; z++) {
for (int x = 1; x <= path; x++) {
for (int y = 1; y <= ncity; y++) {
printf("\n\t\t %s", cityname[x][y]);
printf("\n\t\t %f km", distance[x][y]);
}
}
}
}
This the error that had been listed once i started compiling my code
【问题讨论】:
-
ncity[i]是一个指针。它的类型是int *,您在这里将它与int类型进行比较 -for (int y = 1; y <= ncity[i]; y++)。 -
在索引数组时要小心循环变量范围。在这种情况下,它们几乎总是应该从
0开始而不是1,并以< limit而不是<= limit结束。 -
旁白:请养成输出换行符last而不是first的习惯。