简体   繁体   English

为什么在尝试传递数组值时会出现“不兼容的指针类型”?

[英]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.

相关问题 为什么我从不兼容的指针类型警告中得到初始化? - Why am I getting an initialization from incompatible pointer type warning? 为什么会收到“不兼容的指针类型”警告? - Why am I getting 'Incompatible pointer type' warning? 为什么我从该结构中收到不兼容的指针类型警告? - Why am I getting an incompatible pointer type warning from this structure? 为什么我收到“来自不兼容指针类型的分配”错误? - Why am I getting an “assignment from incompatible pointer type” error? 为什么我会收到“警告:来自不兼容指针类型的分配”? 结构数组中的双链表 - Why am i getting “warning: assignment from incompatible pointer type”? Double linked list in a struct array 当我尝试使用 strncpy 将一个数组缓冲区复制到另一个数组缓冲区时,为什么会出现不兼容的指针类型错误? - Why am I getting Incompatible pointer type error as I try to copy one array buffer into another using strncpy? 为什么我会收到不兼容的指针类型错误? - Why am I getting an incompatible pointer types error? 将指针分配给多维数组时,为什么会出现“不兼容的指针类型” - Why do I get “incompatible pointer type” when assigning pointers to a multidimensional array 我正在尝试使用双指针将字符串传递给 function 但出现错误 - I am trying to pass a string into a function with a double pointer but getting an error 将值写入数组时为什么会出现错误? - Why am I getting an error when writing values to an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM