[英]Why am I getting "incompatible pointer type" when trying to pass array values?
I get an error/warning when trying to get the minimum/maximum values from the points in the main function.尝试从主函数中的点获取最小值/最大值时收到错误/警告。 How can I calculate the max/min points?
如何计算最大/最小点数? Is there any easier method?
有没有更简单的方法? Am i supposed to use a structure?
我应该使用结构吗?
test.c:73:14: warning: incompatible pointer types passing 'int [100][100]' to parameter of type 'int *' [-Wincompatible-pointer-types] convexHull(points, n); test.c:33:21: note: passing argument to parameter 'point' here void convexHull(int point[], int n) ^ 1 warning generated.**
void convexHull(int point[], int n)
{
int i;
//n is the size
int min_x=0;
int max_x=0;
if (n <3)
{
printf("Convex hull can't have less than 3 points\n.");
}
//Finding point with min/max x-coordinate.
for (i=1;i<n;i++)
{
if (point[i] < point[min_x])
{
min_x=i;
}
if (point[i] > point[max_x])
{
max_x = i;
}
}
printf("%d\n",max_x);
printf("%d\n",min_x);
}
int main()
{
int n;
int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3}};
n = sizeof(points)/sizeof(points[0]);
convexHull(points, n);
return 0;
}
The array points
is declared as a two-dimensional array数组
points
被声明为二维数组
int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3}};
So used in an expression as a function argument it is implicitly converted to pointer to its first element of the type int ( * )[100]
.因此,在表达式中用作函数参数时,它被隐式转换为指向其
int ( * )[100]
类型的第一个元素的指针。
However the corresponding function parameter has the type int *
但是相应的函数参数的类型为
int *
void convexHull(int point[], int n)
because the parameter declared like int point[]
is adjusted by the compiler to the declaration int * point
.因为声明为
int point[]
的参数被编译器调整为声明int * point
。
And there is no implicit conversion from the type int ( * )[100]
to the type int *
.并且没有从
int ( * )[100]
类型到int *
类型的隐式转换。
Moreover it seems this declaration而且似乎这个声明
int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3}};
does not make sense because each "point" has only twp elements as for example {0, 3}
but not 100 elements.没有意义,因为每个“点”只有 twp 个元素,例如
{0, 3}
而不是 100 个元素。
What you need is to declare a structure for example like您需要的是声明一个结构,例如
struct Point
{
int x;
int y;
};
and use it in your program.并在您的程序中使用它。
In this case the array of points can be defined the following way在这种情况下,可以通过以下方式定义点数组
struct Point points[] = { {0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3} };
So the function declaration and definition should be changed accordingly.所以函数声明和定义应该相应地改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.