Question about DirectInput8Create

Started by
5 comments, last by pghTech 17 years, 7 months ago
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.
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.
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?
&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.
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;
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.
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.

This topic is closed to new replies.

Advertisement