• 13
• 18
• 19
• 27
• 10

# Question about pointers to VOID...

This topic is 3834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello! I'm writing a DirectInput callback function that includes data found in a struct elsewhere in my program (DXStuff). I'd like to send this callback function a pointer to this struct. Here's what my callback function is called...
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
VOID* pContext )
{
// Write to DXStuff through pContext...
pContext->DXStuff->structVariable = 1;
}
and here's a simplified form of the function that will be calling it:
HRESULT Init_JoyInput(struct DirectStuff *DXStuff)
{
// Enumerate joysticks...
if( FAILED( DXStuff->g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
NULL, DIEDFL_ATTACHEDONLY ) ) )
{
// DirectInput couldn't enumerate anything.  Something wrong?
return E_FAIL;
}
return S_OK;
}
I'd like to send a pointer to DXStuff through "VOID* pContext", but I don't know how to do that. Would any of you show me how to do this? Thanks in advance for the help!

##### Share on other sites
void fun(int* foo) {  if (!foo) {    assert(false && "Invalid pointer\n");    return;  }  printf("%d\n", *foo);  }void fun(void* tmp) {  int* foo = reinterpret_cast<int*>(tmp);  fun(foo);}void bar() {  int value = 7;  int* ptr = &value;  void* void_ptr = reinterpret_cast<void*>(ptr);  fun(void_ptr);}

That make sense?

##### Share on other sites
Just posted in your other thread... You really shouldn't post the same thread in multiple forums. If you put it in the wrong one, I am sure a mod would be able to help you move it.

##### Share on other sites
HRESULT Init_JoyInput(struct DirectStuff *DXStuff){	// Enumerate joysticks...	if( FAILED( DXStuff->g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, 		EnumJoysticksCallback,		(void*)DXStuff, DIEDFL_ATTACHEDONLY ) ) )	{		// DirectInput couldn't enumerate anything.  Something wrong?		return E_FAIL;	}	return S_OK;}BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,		VOID* pContext ){       DirectStuff *D=(DirectStuff*)(pContext);	// Write to DXStuff through pContext...	D->structVariable = 1;}

I've used C style casts since you said "struct DirectStuff *DXStuff" in your parameter declaration.

##### Share on other sites
Thanks for the replies, guys! Yes I did post the same question in the DirectX forum too, but I'm glad I posted here because NotAYakk gave me some info that helps me to understand what's behind the 'void' concept. But next time if I post in the wrong forum, I'll just ask to have the post moved to the more proper forum.

Thanks again!

##### Share on other sites
Don't use void* pointers if you can, they just hide compile-time errors (easy to fix) and make them run-time bugs (hard to fix).

You can't use void* for anything useful - you can just store them and return them later untouched. For anything useful you need to cast them into pointer to something, and if you know what this something is you may as well use the Something* type instead of void*. This will give you a compile error if someone passes an incompatible pointer.

In your case, why not have the function accept a DXStuff* ?

[Edited by - Iftah on September 19, 2007 6:45:35 AM]

##### Share on other sites
Quote:
 Original post by IftahIn your case, why not have the function accept a DXStuff* ?

Because it is a call back function called internally by DirectX's EnumDevices(), which obviously defines the function type and is beyond our control.

These are quite prevalent in DirectX's various enumeration functions and an example of one area that we still can't avoid the dreaded void*.

##### Share on other sites
Do try the trick I referred to in my code.

Have a void* version of the function that does nothing except recast the parameters, and call the "correct" version of the function. This removes clutter from the "real" function, and any compiler worth it's salt will optimize out the call in any case.