1 1 vote 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 Programming in C + – A_i_$_h 2.0k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
0 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)); } Aashish S answered Jun 15, 2017 • edited Jun 15, 2017 by Aashish S Aashish S comment Share Follow See all 13 Comments 13 13 Comments reply A_i_$_h commented Jun 15, 2017 reply Follow flag Int *p; p=&a[2][2][2]; //this assignment is wrong? Please can u explain 0 0 replyShare A_i_$_h commented Jun 15, 2017 reply Follow flag In method 1, considering everything mentioned to be the same and only printf changed to print **p and *p , what would the respective answers be 0 0 replyShare Arjun commented Jun 15, 2017 reply Follow flag a[2][2][2] - is outside the array bound of int a[2][2][2] as the array index in C starts from 0. So, *p is invalid memory access - probably should cause a segmentation fault and sometimes works fine. But "garbage value" is not the correct answer though we can get it if no segmentation error happens. 1 1 replyShare Aashish S commented Jun 15, 2017 i edited by Aashish S Jun 15, 2017 reply Follow flag p=&a[2][2][2]; as shown in 3D array diagram and also explained by Arjun Sir scope is out of array index as it varies from a[0][0][0] to a[1][1][1] .. so will print some garbage value but at same time u can point pointer to point int *p; p=&a[1][1][1]; will print value = 8 - here the pointer will only point to cell a[1][1][1] but if you write following code then int *p; p=&a[0][0][0]; for (int i=0; i<8;i++) { printf(" %d ",*(p+i)); // OUTPUT will be 10 2 3 4 5 6 7 8 } its bcoz int *p = (int*)(&a); is equal to int *p=&a[0][0][0]; bcoz simply "a" is equal to writing a[0][0][0] bcz name of array is pointer to 1st element in the array 0 0 replyShare Aashish S commented Jun 15, 2017 reply Follow flag In method 1, **p and *p ,will point to address of memory location a[0][0][0] you try this code in programming... 0 0 replyShare A_i_$_h commented Jun 15, 2017 reply Follow flag int(*p)[2][2]=&a; int(*p)[2][2]=a; int ***p=(int*)(&a); int(**p)[2]=a; among these which are the right ones and the wrong ones if it's wrong please do tell why it is 0 0 replyShare Aashish S commented Jun 15, 2017 i edited by Aashish S Jun 15, 2017 reply Follow flag Only int(*p)[2][2]=a; is Correct 1. &a means address of a itself not address of array stored in a 3. one cannot declare pointer to 3D array in this fashion ( bcz ***p means pointer to pointer to pointer to address of array ) ie., int * cannot be used to initialize an entity of type int *** 4 . same as point 3 ie., value of type int (*)[2][2] cannot be used to initialize an entity of type int (**)[2] 0 0 replyShare A_i_$_h commented Jun 15, 2017 reply Follow flag So int ***p=(int ***)(&a); is correct? If wrong then what's the correct way with the left side of the operator as int***p? then in that case int *p=&a; should also be wrong? (Considering a to be a one dimensional array) becz &a is address of a itself and not the array address 0 0 replyShare Aashish S commented Jun 16, 2017 reply Follow flag int ***p=(int ***)(&a); is correct syntactically int *p=&a; is not correct rather it should be int *p=a; 0 0 replyShare A_i_$_h commented Jun 16, 2017 reply Follow flag but programs do run when given as int*p=&a; how? or is it correct because its given in different lines int *p; p=&a; ?? 0 0 replyShare Aashish S commented Jun 16, 2017 reply Follow flag i think you know better now so try urself... as programming is never done by asking doubts its rather done by practising and asking your doubts to computer..dont waste time ur precious time in questioning 0 0 replyShare A_i_$_h commented Jun 16, 2017 reply Follow flag okay :) 0 0 replyShare Aashish S commented Jun 16, 2017 reply Follow flag for ur doubt int*p=&a; is correct or not - read the previous comments carefully the answer there... 0 0 replyShare Please log in or register to add a comment.
0 0 votes p=&a[0][0][0]; now printf("%d", *p); will give u the 10 Saurabh_Verma answered Sep 4, 2017 Saurabh_Verma comment Share Follow 0 reply Please log in or register to add a comment.