Tuesday, November 12, 2013

CPP Series: Pointer to Pointer

Everytime..... this pointer fella give me headache... fuhhh...

here the reference for future headache [link].

This function modify the variable piStuff inside the function putStuff.

Example pointer to double pointer:

int **piStuff;
putStuff(&piStuff);

void putStuff(int ***ppiStuff)
{
    *ppiStuff = new int* [SIZE_A];
     for(int i=0; i<SIZE_A;i++) {
          (*ppiStuff)[i] = new int [SIZE_B];
     }

     for(int i=0; i
<SIZE_B;i++) {          
          for(int j=0; j<SIZE_B;j++) {
              (*ppiStuff)[i][j] = iSomeVar;
          }
     }
}


Example pointer to pointer:

int *piStuff;
putStuff(&piStuff);

void putStuff(int **ppiStuff)
{
    *ppiStuff = new int [SIZE];

     for(int j=0; j
<SIZE;j++) {         
         (*ppiStuff)[j] = iSomeVar;
     }    
}


No comments: