C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 11)
                   
                                       
                                11.
What will be the output of the program?
 
                                    #include<stdio.h>
int main()
{
    int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
    int *p, *q;
    p = &arr[1][1][1];
    q = (int*) arr;
    printf("%d, %d\n", *p, *q);
    return 0;
}
Discussion:
58 comments Page 1 of 6.
                
                        RAHUL SHARMA said: 
                         
                        2 decades ago
                
                Here the 3-D array the value can be store as:
and the *arr represent the first number of a array is 10.
Then hence the number is (8,10).
                a[2][2][2]={
             {
               10,2
               3,4   // First Block
             }
             {
                5,6
                7,8  //Second Block
             }
           }
a[ 0 ] [ 0 ] [ 0 ] =10;
   |     |     |
Block  Rows  Columns
a [ 1 ] [ 1 ] [ 1 ] = 8;      
    |     |     |
 2'Block 2'Row 2'column           and the *arr represent the first number of a array is 10.
Then hence the number is (8,10).
                     (38)
                
            
                        Kishor said: 
                         
                        1 decade ago
                
                Elements stored as:
arr[0][0][0] = 10
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
So *p=arr[1][1][1]=8.
And *q=10(first element).
                arr[0][0][0] = 10
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
So *p=arr[1][1][1]=8.
And *q=10(first element).
                     (4)
                
            
                        Chetan said: 
                         
                        1 decade ago
                
                arr[2][2][2], this array representation says that there are 2Blocks  each with 2Rows and 2Columns 
i,e we can judge that every Block will have 4 Elements each
so Block1-------[10 2 3 4]
i,e {10 2} in Row1
{3 4} in Row2
Again, Block2----[5 6 7 8]
i,e {5 6} in Row1
{7 8} in Row2
Now, let me explain how elments are described in 3D array representaion:- [Block][Row][Column]
so, the member of Block1 are described as below,
10 is [0][0][0]
2 is [0][0][1]
3 is [0][1][0]
4 is [0][1][1]
now, the members of Block2,
5 is [1][0][0]
6 is [1][0][1]
and so on
Thus, arr[1][1][1] is 8
                i,e we can judge that every Block will have 4 Elements each
so Block1-------[10 2 3 4]
i,e {10 2} in Row1
{3 4} in Row2
Again, Block2----[5 6 7 8]
i,e {5 6} in Row1
{7 8} in Row2
Now, let me explain how elments are described in 3D array representaion:- [Block][Row][Column]
so, the member of Block1 are described as below,
10 is [0][0][0]
2 is [0][0][1]
3 is [0][1][0]
4 is [0][1][1]
now, the members of Block2,
5 is [1][0][0]
6 is [1][0][1]
and so on
Thus, arr[1][1][1] is 8
                     (2)
                
            
                        Punit said: 
                         
                        1 decade ago
                
                Why the statement q = (int*) arr? Why this type casting is done?
                
                     (1)
                
            
                        Shyamal said: 
                         
                        7 months ago
                
                [2][2][2]=8 position 
[1][1][2]=1 position
Answer is 8,10.
                [1][1][2]=1 position
Answer is 8,10.
                     (1)
                
            
                        ArunKumar A said: 
                         
                        12 months ago
                
                @All.
arr[0][0][0]={10, 2};
arr[0][0][1]={3, 4};
arr[0][1][1]={5, 6};
arr[1][1][1]={8, 0};
                arr[0][0][0]={10, 2};
arr[0][0][1]={3, 4};
arr[0][1][1]={5, 6};
arr[1][1][1]={8, 0};
                     (1)
                
            
                        Anmol singh said: 
                         
                        1 year ago
                
                Can anyone help me how 8 came? Anyone, please explain.
                
                     (1)
                
            
                        Nik said: 
                         
                        4 years ago
                
                Thanks @Rahul.
                
                     (1)
                
            
                        Deepak said: 
                         
                        7 years ago
                
                Can anyone explain this more clearly to get this?
                
                     (1)
                
            
                        Arvindh said: 
                         
                        1 decade ago
                
                I feel the best way to think of 3D arrays is to imagine a cube and put the numbers in their corresponding vertices.
For 2x2x2 array, we can imagine a cube with a total of 8 vertices, 4 in the front and 4 at the back.
 
10, 2, 3, 4, 5, 6, 7, 8 can be written here as
  
10(5) 2(6)
3(7) 4(8)
/*
Where the numbers without brackets are the vertices of the nearer face of the cube and those in brackets are the vertices of the farther face.
*/
Now, a[1][1][1] is the bottom right vertex of the farther face of the cube -- which is 8.
And "arr" is the address of the first element of the array -- here it is 10.
So, we get 10 and 8 as the answers.
                For 2x2x2 array, we can imagine a cube with a total of 8 vertices, 4 in the front and 4 at the back.
10, 2, 3, 4, 5, 6, 7, 8 can be written here as
10(5) 2(6)
3(7) 4(8)
/*
Where the numbers without brackets are the vertices of the nearer face of the cube and those in brackets are the vertices of the farther face.
*/
Now, a[1][1][1] is the bottom right vertex of the farther face of the cube -- which is 8.
And "arr" is the address of the first element of the array -- here it is 10.
So, we get 10 and 8 as the answers.
                     (1)
                
            Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers