Having Fun with Pointers {D3D}

I basically created a chain of pointers...Is there any purpose to having something like this? I saw in DirectX API there was a function that requires an address of a pointer so I made this to practice..What is the purpose of something like this..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

//Purpose to create a functions that return pointers to pointers
int** myfunc(int** );
int*** myfunc2(int ***);

int main ()
{
    int x = 5;
    
    int* ptr = &x;
    int** ptrp;
    int*** ptrpp;
    
    cout << "First Pointer Address   " << &ptr << endl;
    
    ptrp = myfunc(&ptr);
    
    cout << endl;
    cout << "Second Pointer Address: "<< &ptrp << endl;
    
    ptrpp = myfunc2(&ptrp);
    
    cout << endl;
    cout << "Third Pointer Address:  " << &ptrpp << endl;
    cout << endl << endl;
    return 0;
}

int** myfunc(int** intpp)
{
    return intpp;
}

int*** myfunc2(int *** intppp)
{
    
    return intppp;
}
Last edited on
You have become a three-star programmer. Good work.

Really, there is little to no point, especially in C++. The only time I think it might be useful is if you needed to take a two-dimensional array of polymorphic objects for some reason, though there are better ways of doing that too.
Thanks but DirectX uses pointers to pointers quite a bit...And I would like to know what the purpose is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ptrp = myfunc(&ptr);
ptrpp = myfunc2(&ptrp);

....

int** myfunc(int** intpp)
{
    return intpp;
}

int*** myfunc2(int *** intppp)
{
    
    return intppp;
}


I know this is a very trivial program but why not do
1
2
ptrp = &ptr;
ptrpp = &ptrp;
instead? I see no point in a function's only purpose to return the value passed without doing anything to the value.
@giblit I was just having fun with creating the functions that accept the different types of pointers and returning them..

This is because I am studying DirectX API..and I see that there are functions that take in pointers to pointers and return them..So I wanted to create my own functions that do the same thing to get a better understanding of the syntax..

Now I know how it does it by why does it? What is the need for it?

For Example: The SWAP CHAIN


HRESULT IDXGIFactory::CreateSwapChain(IUnknown *pDevice, DXGI_SWAP_CHAIN_DESC *pDesc, IDXGISwapChain **ppSwapChain);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void myfunc(int** ppSwapChain);

int main ()
{
    int x = 5;
    int*ptr = &x;
    int** ppSwapChain = &ptr;
    
    myfunc(ppSwapChain);
    
    return 0;
}

void myfunc(int** ppSwapChain)
{
    cout << ppSwapChain << endl;
}
Last edited on
Because ppSwapChain is an out parameter that sets a pointer. They could have used a IDXGISwapChain *&, but they didn't.
Thanks for your input helios but I want to get a fundamental understanding of how and why the function is structured the way it is...Breaking it apart and understand how the swapChain is coded..Understanding fundamentally why it is a double pointer...the bits and bites if you will.

I want to find somewhere that explicitly tells me about this function and its parameters
Last edited on
helios actually did say why it was a double pointer:
helios wrote:
ppSwapChain is an out parameter that sets a pointer
Have you tried the documentation maybe?
http://bit.ly/1sYTpBh

MSDN wrote:
A pointer to a variable that receives a pointer to the IDXGISwapChain interface for the swap chain that CreateSwapChain creates.
Is it like creating an instance of a class kinda? Why can't i just use the pointer thats already there...why do I have to create a pointer to another pointer?

This is what I'm trying to ask..

YES I AM A FREAKIN NEWB...
Last edited on
This type of programming is so new to me...
You don't need to create a pointer. You can simply do
1
2
IDXGISwapChain *p;
/*...*/ CreateSwapChain(/*...*/, &p);
you seem to know a lot about directX how did you learn? I sure you had other sources than just the documentation?
I have never used DirectX. Nothing I've said so far is particularly strange for a C++ API.
I knew that it was a out pointer but i just didn't know why...It was so simple ..

"The function must give an IDrawable pointer back to the caller. But the function's return value is already used for the error/success code. Therefore, the pointer must be returned through an argument to the function."

http://i.msdn.microsoft.com/dynimg/IC389995.png



http://msdn.microsoft.com/en-us/library/windows/desktop/ff485840(v=vs.85).aspx
Last edited on
Topic archived. No new replies allowed.