759 views
1 votes
1 votes
Int a[2][2][2]={{10,2,3,4},{5,6,7,8}};

int *p;

p=&a[2][2][2];

printf("%d",*p);

this gives garbage value,what should be the changes to get p value as 10

2 Answers

0 votes
0 votes
a[0] a[1]
a[0][0] a[0][1] a[1][0] a[1][1]
a[0][0][0] a[0][0][1] a[0][1][0] a[0][1][1] a[1][0][0] a[1][0][1] a[1][1][0] a[1][1][1]
10 2 3 4 5 6 7 8

In 3D Arrays

a[i][j][k] = *(a[i][j] + k) = *(*(a[i] + j ) + k)  =  *(*(*(a + i) + j ) + k)

and pointer to 3D array is 

METHOD 1 :    int (*p)[2][2] = a;

To get value of 10 ie a[0][0][0]

i=0 , j=0 , k=0

*(*(*( a + 0) + 0) + 0) = ***a    OR    *(*(*( p + 0) + 0) + 0) = ***p

METHOD 2 :   int *p = (int*)(&a);

for (i=0;i<8;i++)
    {
        printf("%d\n",*(p + i));
    }
 

edited by

No related questions found