Sign in to follow this  
hkBattousai

A quick question about pointers

Recommended Posts

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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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*).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this