A quick question about pointers

Started by
5 comments, last by Zahlman 14 years, 8 months ago
HWND * phwnd, hwnd;
phwnd = (HWND *) AppFrame::GetHandle();
hwnd = *phwnd;
MessageBox(hwnd, _("Text"), _("Title"), MB_OK);
The first parameter of MessageBox() must be a HWND value, and Get handle returns a value of void pointer value. I defined "phwnd" and "hwnd" to match these two types. Do you have any idea how to reduce this 4-line code to a single line? I also tried
MessageBox((HWND) *AppFrame::GetHandle(), _("Text"), _("Title"), MB_OK); // Line #118
It gives the following error message:
Quote:C:\...\AppMain.cpp|118|error: `void*' is not a pointer-to-object type|
IDE: Code::Blocks (wxSmith) Compiler: wxWidgets
Advertisement
MessageBox(*(HWND*)AppFrame::GetHandle(), _("Text"), _("Title"), MB_OK);
Quote:Original post by Evil Steve
MessageBox(*(HWND*)AppFrame::GetHandle(), _("Text"), _("Title"), MB_OK);


Woow, it works like a charm!
I still have a lot to learn about pointers...

But, why doesn't it works this way?
MessageBox((HWND)*AppFrame::GetHandle(), _("Text"), _("Title"), MB_OK);
Quote:Original post by Battousai
But, why doesn't it works this way?
MessageBox((HWND)*AppFrame::GetHandle(), _("Text"), _("Title"), MB_OK);

Because *AppFrame::GetHandle() is dereferencing a void pointer, which you can't do.
Quote:Original post by Battousai
IDE: Code::Blocks (wxSmith)
Compiler: wxWidgets


Minor correction:

Compiler: probably MinGW, which comes bundled in one of the downloads packets of code::blocks (btw, it is the Windows port of GCC).

wxWidgets is the widget-toolkit you use.
Quote:Original post by Battousai
But, why doesn't it works this way?
MessageBox((HWND)*AppFrame::GetHandle(), _("Text"), _("Title"), MB_OK);

This equals
(HWND)(*any_pointer)
so the * has a different meaning because it dereferences the pointer it preceeds. And as already stated with void pointers this is not a valid operation because there is no type "void" you can use a variable type. Anyway, dereferencing is not what you want in this case.

What you want is
(HWND*)(any_pointer)
whereas the * now belongs to the HWND declaration saying that you want to cast the type of any_pointer to (HWND*).

------------------------------------I always enjoy being rated up by you ...
I have a key to a cage at the zoo where a cute, harmless monkey is kept. If I want you to get the monkey for me, I had better tell you first that the key is for the monkey's cage, and would you please fetch it for me, rather than just giving you the key and assuming (because I know where the key leads) that you will bring me back the monkey. You're not going to know what kind of cage to open, and might well be frightened that I've sent you off to fetch some ferocious beast.

So it is with the void pointer. '*((HWND*)x)' means "treat x as a pointer to a HWND, and then please dereference it". '(HWND)(*x)' means "Please dereference x, and treat the result as an HWND". "The result" has has no type the latter way. It is too late to say what kind of thing is pointed at *after* dereferencing the pointer.

This topic is closed to new replies.

Advertisement