• Advertisement
Sign in to follow this  

Question about DirectInput8Create

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the DirectInput8Create function, it takes the following 2nd to last argument: [ppvOut] Address of a pointer to a variable to receive the interface pointer if the call succeeds. In my program I pass for that argument (void**)m_di and m_id is defined as: IDirectInput8 *m_di; Therefore, I was just hoping someone could explain how that "pointer to a variable to receive the interface poiinter" works (like a double pointer?) I don't see where I setup a double pointer to begin if m_di is just a pointer to the IDirectInput8 object to begin with.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by pghTech
In my program I pass for that argument (void**)m_di
I'm surprised that doesn't blow up in your face. You should be using (void**)&m_di for the last argument.

Share this post


Link to post
Share on other sites
Yes, my face is on fire.

Sorry, it was a typo - should be a reference &m_di.

But could someone actually help me out with the OP question?

Share this post


Link to post
Share on other sites
&m_di is the address of the pointer to the IDirectInput8 interface. So it's a double pointer.
By passing the address of the pointer to the function, the function can change the pointer to point at something else - the newly created IDirectInput8 interface.

Share this post


Link to post
Share on other sites
Thanks Evil Steve:

Just so I understand; since m_di was defined as

IDirectInput8 *m_di

I am assuming at this stage it in itself is not a double pointer and just a pointer to an IDirectInput8 object.

BUT! would it be safe to say that by declaring the argument passed as

(void**)&m_di

that you are passing a sort of a imaginary "second" pointer that points to the pointer &m_di, which in itself, points to the IDirectInput8 object?

I guess what is confusing me, is how the variable m_di is defined as a pointer, but then with no extra "defining" steps, it becomes a double pointer without having to declare a double pointer that points to &m_di.

I am assuming for it to work it would have to be defined like below.
EXAMPLE:

int var1;
int *ptr1;
int **dptr2;

var1 = 10
ptr1 = &var1;
*ptr2 = *ptr1 + 10;

**dptr2 = *ptr1 + 5;

Share this post


Link to post
Share on other sites
Quote:
Original post by pghTech
Just so I understand; since m_di was defined as

IDirectInput8 *m_di

I am assuming at this stage it in itself is not a double pointer and just a pointer to an IDirectInput8 object.
Yup, exactly.

Quote:
Original post by pghTech
BUT! would it be safe to say that by declaring the argument passed as

(void**)&m_di

that you are passing a sort of a imaginary "second" pointer that points to the pointer &m_di, which in itself, points to the IDirectInput8 object?
Sort of. & on the left of a variable means "Address of". Since m_di is already a pointer, you're taking the "Address of" the pointer, and passing the result of that expression to DirectInput8Create(). That's how I always think of it anyway.

Quote:
Original post by pghTech
I guess what is confusing me, is how the variable m_di is defined as a pointer, but then with no extra "defining" steps, it becomes a double pointer without having to declare a double pointer that points to &m_di.
This:
IDirectInput8* m_di;
DirectInput8Create(h, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_di, NULL);
is the same as:
IDirectInput8* m_di;
IDirectInput8** pdi = &m_di;
DirectInput8Create(h, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pdi, NULL);
which is also the same as:
IDirectInput8* m_di;
IDirectInput8** pdi = &m_di;
void** pvoid = (void**)pdi;
DirectInput8Create(h, DIRECTINPUT_VERSION, IID_IDirectInput8, pvoid, NULL);


The point I'm making is that you're not converting the single pointer (m_di) into anything else. You're taking the address of that pointer as a seperate, temporary variable, and passing that double-pointer to the function.

Share this post


Link to post
Share on other sites
Thanks so much Evil Steve. That complete cleared that up.

I understand now that with (void**)&m_di I am passing the address of that pointer(m_di), which obviously, being a pointer points to something. So as you said a temporary pointer to a pointer to an (in this case and object).

Thanks again for the detailed, helpful answer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement