Search code examples
c++cwinapigdi

CreatePolygonRgn and const POINT *


I give you only the lines concerning my problem. I don't know why this does not compile:

POINT ptVertex[5];

ptVertex[0].x = 180;
ptVertex[0].y = 80;
ptVertex[1].x = 100;
ptVertex[1].y = 160;
ptVertex[2].x = 120;
ptVertex[2].y = 260;
ptVertex[3].x = 240;
ptVertex[3].y = 260;
ptVertex[4].x = 260;
ptVertex[4].y = 160;

CreatePolygonRgn( &ptVertex, 5, ALTERNATE )

I compile on DevC++ TDM GCC 64bits and the error is:

cannot convert 'POINT ()[5] {aka tagPOINT ()[5]}' to 'const POINT* {aka const tagPOINT*}' for argument '1' to 'HRGN__* CreatePolygonRgn(const POINT*, int, int)'

If someone can find my mistake. Thanks.


Solution

  • CreatePolygonRgn() expects a pointer to the first POINT in an array, and the number of items in the array. But you are passing it a pointer to the array itself, not its first element. You can either:

    1. index to the first element of the array before applying the & operator:

      CreatePolygonRgn( &ptVertex[0], 5, ALTERNATE )
      
    2. Simply remove the & operator altogether, as a static array can degrade into a pointer to its first element:

      CreatePolygonRgn( ptVertex, 5, ALTERNATE )