small problem with source code

Started by
5 comments, last by pulpfist 17 years, 11 months ago
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????
Advertisement
I believe that indicates a pointer to a pointer.
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 ?
You are expected to pass a reference to a pointer to an IDirect3DDevice9 object:

IDirect3DDevice9 *d3d9;...InitD3D(hInstance, 640, 480, false, deviceType, &d3d9);
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.
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,
Here is a post I made earlier about why pointers sometimes must be converted to pointer-to-pointer when passed as arguments

This topic is closed to new replies.

Advertisement