![](/img/trans.png)
[英]Why am I getting an initialization from incompatible pointer type warning?
[英]Why am I getting "incompatible pointer type" when trying to pass array values?
尝试从主函数中的点获取最小值/最大值时收到错误/警告。 如何计算最大/最小点数? 有没有更简单的方法? 我应该使用结构吗?
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;
}
数组points
被声明为二维数组
int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3}};
因此,在表达式中用作函数参数时,它被隐式转换为指向其int ( * )[100]
类型的第一个元素的指针。
但是相应的函数参数的类型为int *
void convexHull(int point[], int n)
因为声明为int point[]
的参数被编译器调整为声明int * point
。
并且没有从int ( * )[100]
类型到int *
类型的隐式转换。
而且似乎这个声明
int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3}};
没有意义,因为每个“点”只有 twp 个元素,例如{0, 3}
而不是 100 个元素。
您需要的是声明一个结构,例如
struct Point
{
int x;
int y;
};
并在您的程序中使用它。
在这种情况下,可以通过以下方式定义点数组
struct Point points[] = { {0, 3}, {2, 2}, {1, 1}, {2, 1},
{3, 0}, {0, 0}, {3, 3} };
所以函数声明和定义应该相应地改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.