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 5 of 8.
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?
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)
Rashmi said:
1 decade ago
Can anyone tell me.
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
What is meant by last line?
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
What is meant by last line?
Vinod said:
1 decade ago
Substitute p=1 and i and j as 00,01,10,11 in all the arrays condition in printf.
So that for i=0 and j=0, p=1(every time) we get it as 1111.
Repeat the procedure for all. The answer is correct only.
So that for i=0 and j=0, p=1(every time) we get it as 1111.
Repeat the procedure for all. The answer is correct only.
Sourabh said:
1 decade ago
What may be the answer if we solve it by as two dimensional array?
Reena said:
1 decade ago
@Anushu.
The answer correct is:
1111
2222
2222
3333
The given answer is correctly.
The answer correct is:
1111
2222
2222
3333
The given answer is correctly.
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)
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.
Shubham said:
1 decade ago
Can anyone please explain static int a[2][2] = {1, 2, 3, 4}; ?
Also static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
Also static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
Prabha said:
1 decade ago
Why p=0 ?
int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
What is this? Can anyone explain in clearly?
int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
What is this? Can anyone explain in clearly?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers