-2

I am trying to use the PtInRect function in Win32, which takes a POINT object. I have x and y coordinates, but I don't know how I create a POINT from those. I tried looking it up, but could only find this documentation, which does not contain the information I want.

Thank you for your help.

  • 3
    Its a struct, so its created the same way as any other struct. – tkausl Mar 10 '21 at 23:35
  • @tkausl I suppose I could just do `POINT pt = (POINT*)malloc(sizeof(POINT));` and then set the x and y, but I was wondering if there a a function to do this (like `CreateWindow` for windows) – professionalgrammer Mar 10 '21 at 23:39
  • 1
    are you using C++ or C? Because your question is tagged C++ and your comment is using C. – Andy Mar 10 '21 at 23:39
  • 1
    `POINT pt {10,20};` would be a `c++` example. I would avoid `malloc` in `c++` code however the problem with your example is POINT is not a pointer. `PPOINT` is. – drescherjm Mar 10 '21 at 23:41
  • 1
    Allocated on the stack: `POINT pt{10,20};`, allocate on the heap: `auto pt{ new POINT{10,20} };` – Andy Mar 10 '21 at 23:44
  • POINT is not an *object*, it's simply a structure. – Ken White Mar 11 '21 at 01:04
  • @drescherjm That was a typo, and why shouldn't I use `malloc`? – professionalgrammer Mar 11 '21 at 01:15
  • This question should explain that: [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new) – drescherjm Mar 11 '21 at 01:31
  • @drescherjm thanks! So if I wanted to allocate memory for it, then I should use `new` anyway. – professionalgrammer Mar 11 '21 at 01:46
  • It's unreasonable to assume that you can access a C API proficiently when you don't understand the basics of C. Things were better around here back when we enforced the requirement that askers of questions demonstrated basic familiarity with the problem domain. – IInspectable Mar 11 '21 at 09:53

1 Answers1

7

Problem:

You're creating trouble for yourself because of attempting to dynamically allocate the POINT object with malloc. BTW you do not always need dynamic allocation and to initialize any struct you do not need to use malloc as you pointed in the comments, you can just create the object directly.

Solution:

As tkausl pointed in the comments, POINT is created in the same way as any other struct.

Some examples on how creating a POINT:

int main()
{
    int someX = 100;
    int someY = 100;
    RECT testRect1 = {0,0,200,200};
    RECT testRect2 = {150,150,350,350};
    POINT p1{someX,someY};
    POINT p2 = {someX,someY};
    POINT p3;
    p3.x = someX;
    p3.y = someY;
    std::cout << (bool) PtInRect(&testRect1,p1) << std::endl;
    std::cout << (bool) PtInRect(&testRect1,p2) << std::endl;
    std::cout << (bool) PtInRect(&testRect1,p3) << std::endl;
    std::cout << (bool) PtInRect(&testRect1,{someX,someY}) << std::endl;
    std::cout << (bool) PtInRect(&testRect2,p1) << std::endl;
    std::cout << (bool) PtInRect(&testRect2,p2) << std::endl;
    std::cout << (bool) PtInRect(&testRect2,p3) << std::endl;
    std::cout << (bool) PtInRect(&testRect2,{someX,someY}) << std::endl;
}
Gary Strivin'
  • 908
  • 7
  • 20