C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 2)
                   
                                       
                                2.
What will be the output of the program ?
 
                                    #include<stdio.h>
int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), 
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}
Discussion:
78 comments Page 1 of 8.
                
                        Manju said: 
                         
                        1 decade ago
                
                static int a[2][2] = {1, 2, 3, 4};
a[2][2] [ 1 | 2 | 3 | 4 ] memory map
2000 2001 2002 2003[address of array a];
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
p [ 2000 | 2001 | 2002 ] memory map
3000 3001 3002
Step 1:
for loop:
i=0;j=0;
*(*(p+i)+j);
meaning:
content of(content of (3000+0)+j)
content of(2000 + 0)= 1;
*(*(j+p)+i),
cof( cof( 0 + 3000) + i)
cof( 2000 + 0) = 1;
*(*(i+p)+j),
cof( cof (0 + 3000) + 0) =cof ( 2000 + 0)=1;
Step2:
i=0;j=1;
*(*(p+i)+j),
cof (cof (3000 + 0) + j) = cof(2000 + 1)= 2;
*(*(j+p)+i),
cof (cof (1 + 3000) + i) = cof(2001 + 0) =2;/* see memory map*/
*(*(i+p)+j),
cof (cof (0 + 3000) + j) = cof(2000 + 1) =2;
*(*(p+j)+i));
cof (cof (3000 + 1) + i) = cof(2001 + 0) =2;
Step3;2
Step4;3
                a[2][2] [ 1 | 2 | 3 | 4 ] memory map
2000 2001 2002 2003[address of array a];
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
p [ 2000 | 2001 | 2002 ] memory map
3000 3001 3002
Step 1:
for loop:
i=0;j=0;
*(*(p+i)+j);
meaning:
content of(content of (3000+0)+j)
content of(2000 + 0)= 1;
*(*(j+p)+i),
cof( cof( 0 + 3000) + i)
cof( 2000 + 0) = 1;
*(*(i+p)+j),
cof( cof (0 + 3000) + 0) =cof ( 2000 + 0)=1;
Step2:
i=0;j=1;
*(*(p+i)+j),
cof (cof (3000 + 0) + j) = cof(2000 + 1)= 2;
*(*(j+p)+i),
cof (cof (1 + 3000) + i) = cof(2001 + 0) =2;/* see memory map*/
*(*(i+p)+j),
cof (cof (0 + 3000) + j) = cof(2000 + 1) =2;
*(*(p+j)+i));
cof (cof (3000 + 1) + i) = cof(2001 + 0) =2;
Step3;2
Step4;3
                     (11)
                
            
                        Shweta said: 
                         
                        1 decade ago
                
                Here, a[2][2] is a 2D array and int*p is a 1D array of pointers.
So, what is getting stored in p[] is the addresses of the first, second and third elements of a[2][2], that are having difference between consecutive locations as 4 bytes only,
i.e p[0] = &a[0][0],
p[1] = &a[0][1],
p[2] = &a[1][0].
Hence, *p[0] = a[0][0] = 1,
*p[1] = a[0][1] = 2,
*p[2] = a[1][0] = 3.
Also, it is to be noted that in an array p[], *(i+p) is same as *(p+i).
So, *(*(p+i)+j) and *(*(i+p)+j) will evaluate to same value and so does *(*(p+j)+i) and *(*(j+p)+i).
And also, *(p+i) is nothing but the value p[i].
Now, *(*(p+0)+0) = *(p[0]+0) = *(&a[0][0]+0) = *(&a[0][0]) = a[0][0] = 1.
*(*(p+0)+1) = *(p[0]+1) = *(&a[0][0]+1) = *(&a[0][1]) = a[0][1] = 2.
*(*(p+1)+0) = *(p[1]+0) = *(&a[0][1]+0) = *(&a[0][1]) = a[0][1] = 2.
*(*(p+1)+1) = *(p[1]+1) = *(&a[0][1]+1) = *(&a[1][0]) = a[1][0] = 3.
As &a[0][1]+1 will point to the next memory location a[1][0], with the difference in starting address of 4 bytes, and so on.
                So, what is getting stored in p[] is the addresses of the first, second and third elements of a[2][2], that are having difference between consecutive locations as 4 bytes only,
i.e p[0] = &a[0][0],
p[1] = &a[0][1],
p[2] = &a[1][0].
Hence, *p[0] = a[0][0] = 1,
*p[1] = a[0][1] = 2,
*p[2] = a[1][0] = 3.
Also, it is to be noted that in an array p[], *(i+p) is same as *(p+i).
So, *(*(p+i)+j) and *(*(i+p)+j) will evaluate to same value and so does *(*(p+j)+i) and *(*(j+p)+i).
And also, *(p+i) is nothing but the value p[i].
Now, *(*(p+0)+0) = *(p[0]+0) = *(&a[0][0]+0) = *(&a[0][0]) = a[0][0] = 1.
*(*(p+0)+1) = *(p[0]+1) = *(&a[0][0]+1) = *(&a[0][1]) = a[0][1] = 2.
*(*(p+1)+0) = *(p[1]+0) = *(&a[0][1]+0) = *(&a[0][1]) = a[0][1] = 2.
*(*(p+1)+1) = *(p[1]+1) = *(&a[0][1]+1) = *(&a[1][0]) = a[1][0] = 3.
As &a[0][1]+1 will point to the next memory location a[1][0], with the difference in starting address of 4 bytes, and so on.
                     (7)
                
            
                        Dilip said: 
                         
                        1 decade ago
                
                step1:p=0;
step2:i=0,j=0
*(*(p+i)+j)=*(*(p+0)+0)=*((a)+0)=a[0]=1
*(*(j+p)+i)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(i+p)+j)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(j+p)+i)=*(*(1+p)+0)=*((a+1)+0)=a[1]=2
*(*(i+p)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(p+j)+i)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(j+p)+i)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(i+p)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(j+p)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(i+p)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(p+j)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
                step2:i=0,j=0
*(*(p+i)+j)=*(*(p+0)+0)=*((a)+0)=a[0]=1
*(*(j+p)+i)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(i+p)+j)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(j+p)+i)=*(*(1+p)+0)=*((a+1)+0)=a[1]=2
*(*(i+p)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(p+j)+i)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(j+p)+i)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(i+p)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(j+p)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(i+p)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(p+j)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
                     (6)
                
            
                        Astha said: 
                         
                        5 years ago
                
                *(*a+1)+0 means a[1][0] element which is 3. So the output should be;
1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
                1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
                     (3)
                
            
                        Subodh kumar said: 
                         
                        7 years ago
                
                Simple process for solving it;
i < 2
Means i has maximum value i =1
j <2
Same for j
j =1
According to the question;
P + i + j = a[2] = 3.
So, ' C ' is the correct answer.
                i < 2
Means i has maximum value i =1
j <2
Same for j
j =1
According to the question;
P + i + j = a[2] = 3.
So, ' C ' is the correct answer.
                     (2)
                
            
                        Amit said: 
                         
                        4 years ago
                
                @Manju.
You explained it clearly. Thanks.
                You explained it clearly. Thanks.
                     (1)
                
            
                        Priya said: 
                         
                        1 decade ago
                
                Some more clearly please.
                
                     (1)
                
            
                        Anshu said: 
                         
                        1 decade ago
                
                Answer is worng.
Correct answer is.
1 1 1 1.
2 3 2 3.
3 2 3 2.
4 4 4 4.
Execute it on C compiler and check.
                Correct answer is.
1 1 1 1.
2 3 2 3.
3 2 3 2.
4 4 4 4.
Execute it on C compiler and check.
                     (1)
                
            
                        Venkat said: 
                         
                        7 years ago
                
                Thanks @Pradeepa.
                
                     (1)
                
            
                        Mohan said: 
                         
                        6 years ago
                
                @Dilip.
How a+1 points to a[1] ?
a is 2D array
Logically a+1 means 1th 1D array of a.
Please explain briefly.
                How a+1 points to a[1] ?
a is 2D array
Logically a+1 means 1th 1D array of a.
Please explain briefly.
                     (1)
                
            Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers