Navigate through Array by Pointer to Pointer

149 views Asked by At

I have tried to move through array by double pointer. Here is the Code.

void pptr (int **sptr2,int **ptr2)
{
    **ptr2 = (**sptr2 + 7);       //Works Fine
    *sptr2++; *ptr2++;            //Probable problem Statement
    **ptr2 = (**sptr2 + 7);       //Not Workign
}
void ppointer (int *sptr,int *ptr)
{
    pptr (&sptr,&ptr);
}
main()
{
   int sour[2];
   sour[0] = 40;
   sour[1] = 50;
   int var[2];
   var[0] = 10;
   var[1] = 20;
   printf("befor change %d %d\n",var[0],var[1]);

   ppointer(&sour[0],&var[0]);
   printf("pointer to pointer change %d %d\n",var[0],var[1]);
}

I wish to update var in pptr function. I can use double pointer (**sptr,**pptr) to reference pointer (sptr,ptr) (which are pointing to array) into function. I am able to update first one but second one has no change. I think problem is with statement *sptr++ & *ptr++.

Please help me understand how can i navigate through array by double pointer. Thank you

1

There are 1 answers

0
Silentical On

I think it is the compiler just being silly with the "*sptr++;" and the "*ptr++;"

void pptr (int **sptr2, int **ptr2)
{
    **ptr2 = (**sptr2 + 7);       

    ///////////////////////////////////////////////////////
    *sptr2++;    //This is the problem these two statements
    *ptr2++;
    ///////////////////////////////////////////////////////

    **ptr2 = (**sptr2 + 7);
}

Now however if you change it to "*sptr2 += 1;" and "*ptr += 1;" it then works

void pptr (int **sptr2, int **ptr2)
{
    **ptr2 = (**sptr2 + 7);       

    ///////////////////////////////////////////////////////
    *sptr2 += 1;    //Now no problem
    *ptr2 += 1;
    ///////////////////////////////////////////////////////

    **ptr2 = (**sptr2 + 7);
}

I dont really know why the compiler does this due to lack of experience of using the "variable++" operator, I just generally use "variable += 1" instead.