pointers

what is the use of **ptr?
why we use it?
It's dereferencing a pointer to pointer: http://www.cplusplus.com/doc/tutorial/pointers/
It is called double pointer. it is a pointer to a pointer. It is mostly used to create a 2D Array dynamically. for example a matrix.
you can create a 2D array of integers as given below statically if you know the number of rows and columns prior to program execution
int nMatrix[5][6];
using the concept of double pointer you can create such a matrix dynamically as shown below

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
int nColumnSize = 5;
    int nRowSize = 6;
    int** pMatrix = 0;
    pMatrix = new int*[nColumnSize];
    for( int nColumnCnt = 0; nColumnCnt < nColumnSize; nColumnCnt++ )
    {
        int* pRow = 0;
        pRow = new int[nRowSize];
        pMatrix[nColumnCnt] = pRow;
    }

    for( nColumnCnt = 0; nColumnCnt < nColumnSize; nColumnCnt++ )
    {
        for(int nRowCount = 0; nRowCount < nRowSize; nRowCount++ )
        {
            pMatrix[nColumnCnt][nRowCount] = nColumnCnt * nRowCount;
        }
    }

    for( nColumnCnt = 0; nColumnCnt < nColumnSize; nColumnCnt++ )
    {
        for( int nRowCount = 0; nRowCount < nRowSize; nRowCount++ )
        {
            cout<<pMatrix[nColumnCnt][nRowCount]<<"\t";
        }
        cout<<"\n";
    }


it will give the output

0       0       0       0       0       0
0       1       2       3       4       5
0       2       4       6       8       10
0       3       6       9       12      15
0       4       8       12      16      20

this is just a pointer to a pointer ?
then what is the difference between *ptr and **ptr as *ptr is also to point a memory location?


i am confused about its use and why we use it???
More often then not it's used for dynamic allocation of 2d arrays...


What if you needed to allocate an array of c style strings dynamically? You would have to use a pointer to a pointer.
abbi wrote:
what is the use of **ptr?

Notice that when you say **ptr you're actually dereferencing a pointer to a pointer, as Bazzy said. The actual variable name is simply ptr, as you can see from Reannah's post.
i thinks when we need to pint a variable of pointer type we use **ptr?????

Am I right????
You use **ptr when you want to access the object pointed to by the pointer ptr refers to. To make it clearer:

1
2
3
4
5
int a = 1;
int* p = &a;
int** pp = &p;
**pp = 2;
std::cout << a << std::endl;  // this will output the integer 2 


EDIT: fixed a typo in the code
Last edited on
abbi wrote:
then what is the difference between *ptr and **ptr as *ptr is also to point a memory location?


When you say char* ptr, the compiler sets aside a memory location called 'ptr' that will store the memory address of a char.

But what if I wanted to store the location of ptr? I would need to store the memory address of the memory address. I could do so like this:

 
char** ptrptr = & ptr;


Now the computer sets aside a memory location called 'ptrptr' that will store the memory address of a char* which stores the memory address of a char!

So basically I can take the address of a type even if that type is a pointer. So I can have a pointer to a pointer. I can even do this:

 
char******** crazy;
Last edited on
Topic archived. No new replies allowed.