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.
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)
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)
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)
Paul said:
1 decade ago
This is a terrible and confusing example for anyone!
It is a 2-dimensional array so (a + 1) would point to the first element of the second row, which is 3, and (a + 2) would point to the third row but in this case there is no third row.
Only because a, a+ 1 and a + 2 are cast to integer pointers in the line : static int *p[] = {(int*)a, (int*)a+1, (int*)a+2}, this means they increments by the size of an integer, therefore the next element in the array instead of the next row.
Anyone else agree?
It is a 2-dimensional array so (a + 1) would point to the first element of the second row, which is 3, and (a + 2) would point to the third row but in this case there is no third row.
Only because a, a+ 1 and a + 2 are cast to integer pointers in the line : static int *p[] = {(int*)a, (int*)a+1, (int*)a+2}, this means they increments by the size of an integer, therefore the next element in the array instead of the next row.
Anyone else agree?
Charan said:
1 decade ago
a[2][2] is an array stores all elements which is 2D (2-rows,2-columns).
column 0 1
0 row---1 2
1 row---3 4
int *a gives the base address of the first element.
int *a+1 gives the base address of the second element.
int *a+2 gives the base address of the third element.
All the base address are stored in the pointer *p[].
*p[0] gives 1 , *p[1] gives 2 ,*p[2] gives 3.
If we print *p then it will show the base address.
**p --- gives the value 1.
column 0 1
0 row---1 2
1 row---3 4
int *a gives the base address of the first element.
int *a+1 gives the base address of the second element.
int *a+2 gives the base address of the third element.
All the base address are stored in the pointer *p[].
*p[0] gives 1 , *p[1] gives 2 ,*p[2] gives 3.
If we print *p then it will show the base address.
**p --- gives the value 1.
Sindhu said:
1 decade ago
step1:p=0;
step2:i=0,j=0
*(*(p+i)+j)=a[0]=1
*(*(j+p)+i)=a[0]=1
*(*(i+p)+j)=a[0]=1
*(*(p+j)+i)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=a[2]=3
*(*(j+p)+i)=a[2]=3
*(*(i+p)+j)=a[2]=3
*(*(p+j)+i)=a[2]=3
step2:i=0,j=0
*(*(p+i)+j)=a[0]=1
*(*(j+p)+i)=a[0]=1
*(*(i+p)+j)=a[0]=1
*(*(p+j)+i)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=a[2]=3
*(*(j+p)+i)=a[2]=3
*(*(i+p)+j)=a[2]=3
*(*(p+j)+i)=a[2]=3
Sadashiv said:
1 decade ago
One please help me to understood the output of this program.
#include<stdio.h>
void main()
{
long myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
printf("%ld\t",myarr[1][2]);
printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
}
#include<stdio.h>
void main()
{
long myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
printf("%ld\t",myarr[1][2]);
printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
}
Vamsy said:
1 decade ago
From the above program ;
a[0][0]=1 we can write as *(a+0) ; Here 'a' points a[0] and(a+0) to a[0][0]
a[0][1]=2 we can write as *(a+1) ; Here 'a' points a[0] and(a+1) to a[0][0]
a[1][1]=3 " " " " *(*(a+1)+0)
a[1][1]=4 *(*(a+1)+1)
The program goes in this manner.....
a[0][0]=1 we can write as *(a+0) ; Here 'a' points a[0] and(a+0) to a[0][0]
a[0][1]=2 we can write as *(a+1) ; Here 'a' points a[0] and(a+1) to a[0][0]
a[1][1]=3 " " " " *(*(a+1)+0)
a[1][1]=4 *(*(a+1)+1)
The program goes in this manner.....
Kushal baldev said:
9 years ago
Its very simple just apply your basic logic first of all an pointer to array p will be formed and further carry on tasks on the basics for forloop the *p will be {1, 2, 3} apply for loop for I=0 j=0 and j=1 see the total and calculate the index of p and for I=1 j=0 and j=1.
Rupinderjti said:
1 decade ago
@jai -- it's the declaration of an Array of pointers.But here its size is 3 ( static int *p[2]={(int*)a, (int*)a+1, (int*)a+2};).
And the elements in arrays are 3 pointers pointing to first second and third position of elements of an 2-D array named a[2][2] respectively.
And the elements in arrays are 3 pointers pointing to first second and third position of elements of an 2-D array named a[2][2] respectively.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers