获取有关“间接级别不同”的错误
Posted
技术标签:
【中文标题】获取有关“间接级别不同”的错误【英文标题】:Getting error about "differs in level of indirection" 【发布时间】: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的习惯。
【参考方案1】:
通过在循环中使用z <= ncity[point]
,您可以将“z”与存储ncity[pointer]
值的地址的实际编号进行比较。通常它是一个很大的“int”值。我不明白为什么你需要一个指针数组,但要获得ncity[point]
的第一个值,你需要取消引用:*ncity[pointer]
。在您的第三个循环中发生了同样的情况:y <= ncity
。您将“y”变量与ncity
变量的地址数进行比较。再次,相当大的数字,所以很可能 'y' 变量将在 'cityname' 和 'distance' 之外,然后你会得到 'segmentation fault' 错误。要获得“ncity”的整个大小,您需要遍历所有值或仅在第三个循环中使用 path
valiable
for (int y = 1; y <= path; y++)
printf("\n\t\t %s", cityname[x][y]);
printf("\n\t\t %f km", distance[x][y]);
【讨论】:
以上是关于获取有关“间接级别不同”的错误的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.Object 类型无法解析。它是从所需的 .class 文件 android studio 中间接引用的