I am trying to access a 2D array of chars. I have a pointer on right address but somehow dreferencing is not working.
char ary[5][8];
char temp[8];
int i;
char **a_ptr = &ary;
for(i=0; i<5; i++){
sprintf(temp, "0x10%d" , i);
strcpy(ary[i] , temp);
printf("~~~%s@0x%x == 0x%x" , ary[i] , &ary[i] , (a_ptr + i));
}
for(i=0; i<5; i++){//This wont work.
printf("~~~%s@0x%x" , *(a_ptr + i) , (a_ptr + i));
}
Below is the output of this fucniton before it breaks to derefence the pointer.
Output Format : Value@Address
0x100@0x5fbff408 == 0x5fbff408
0x101@0x5fbff410 == 0x5fbff410
0x102@0x5fbff418 == 0x5fbff418
0x103@0x5fbff420 == 0x5fbff420
0x104@0x5fbff428 == 0x5fbff428
As we can see in above output that array values are filled correctly and a_ptr is pointing to correct addresses (&ary[i] == (a_ptr + i)).
But when the pointer is deference then it breaks there. Even using the [] operators does the same.
*(a_ptr + i); //Breaks
a_ptr[i]; //Breaks as well
However, (a_ptr + i) points to the right address.
Thanks,