Jump to content
  • Advertisement
Sign in to follow this  
LordFallout

small problem with source code

This topic is 4437 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 sample code bool d3d::InitD3D(HINSTANCE hInstance,int width, int height, bool windowed,D3DDEVTYPE deviceType,IDirect3DDevice9** device) whats with the double * before device????

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by vinb
I believe that indicates a pointer to a pointer.


o.o i guess that makes sense :P just dont really understand why we would need a pointer to a pointer, also if thats the case can there be a pointer to a pointer to a pointer ?

Share this post


Link to post
Share on other sites
You are expected to pass a reference to a pointer to an IDirect3DDevice9 object:


IDirect3DDevice9 *d3d9;

...

InitD3D(hInstance, 640, 480, false, deviceType, &d3d9);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here, you use a pointer to pass some kind of return value in the functuion's arguments. something like
void getMousePos(int* x,int* y), except that x is not an int, but a pointer. I believe they do this because the COM standard (directX uses COM) use the return value of the methods to return error codes, so they can't just pass the pointer in the return value.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordFallout
Quote:
Original post by vinb
I believe that indicates a pointer to a pointer.


o.o i guess that makes sense :P just dont really understand why we would need a pointer to a pointer, also if thats the case can there be a pointer to a pointer to a pointer ?


The function needs a pointer to a pointer because if fills *device with a pointer value. Consider the following code:

bool my_malloc(unsigned int size, my_struct *p)
{
p = (my_struct*)malloc(size);
if (!p) return false;
return true;
}

When the function exits, p is unmodified because you can't modify a parameter. You can modify the memody where p points, but not p itself. Now, redo it with a pointer to a pointer:

bool my_malloc(unsigned int size, my_struct **p)
{
*p = (my_struct*)malloc(size);
if (!*p) return false;
return true;
}

now, p is not modified, but *p (the memory poinetr by p) is.

d3d::InitD3D(), in your example, works in a similar way. device can't be modified, but *device can, and *device is initialized with a new IDirect3DDevice9*.

You typically use this kind of method in this way:

IDirect3DDevice9* my_device = NULL;
d3d.InitD3D(blah blah blah, &my_device);

This way, my_device will point to the new device object.

Triple pointers exists, but are very rare (and barely readable, so experienced coders tend to avoid them).

Regards,

Share this post


Link to post
Share on other sites
Here is a post I made earlier about why pointers sometimes must be converted to pointer-to-pointer when passed as arguments

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!