C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 12)
12.
What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}
Discussion:
61 comments Page 1 of 7.
Rohan said:
7 years ago
@All.
#include<stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
/* (i) int a[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
-> 2-D array with 3 rows and 4 columns.
-> a[0] [0]= {1}, a[0][1] = {2} , a[0][2] = {3} ,a[0] a[3] = {4}....
and so on........
*/
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
/* (i) a[0] -> memory location begins at 1002
a[0] +1 -> 1002 +4 = 1006 (memory location)
(ii) a[0]+1 -> currently at the memory location
*( a[0]+1 ) -> element at a[0][1] = 2
(iii) *(a+0) -> a[0]
*(a+0)+1 -> a[0][1]
*(*(a+0)+1)) -> *(a[0][1]) = element at a[0][1] = 2
*/
return 0;
}
#include<stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
/* (i) int a[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
-> 2-D array with 3 rows and 4 columns.
-> a[0] [0]= {1}, a[0][1] = {2} , a[0][2] = {3} ,a[0] a[3] = {4}....
and so on........
*/
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
/* (i) a[0] -> memory location begins at 1002
a[0] +1 -> 1002 +4 = 1006 (memory location)
(ii) a[0]+1 -> currently at the memory location
*( a[0]+1 ) -> element at a[0][1] = 2
(iii) *(a+0) -> a[0]
*(a+0)+1 -> a[0][1]
*(*(a+0)+1)) -> *(a[0][1]) = element at a[0][1] = 2
*/
return 0;
}
(5)
PUSHPARAJ said:
5 years ago
*(a[0]+1) is Nothing. Just a[0][1] value is 2.
*(*(a+0)+1)) as same as a[0][1] value is 2.
*(*(a+0)+1)) as same as a[0][1] value is 2.
(4)
Mohan said:
6 years ago
Why this program works differently in 1 dimensional array?
a+0 gives only the base address (i.e) 1002.
*(a+0) also gives the base address (1002) it wouldn't give the value at address (i.e =>1).
*(a+0)+1 => 1002+4 => 1006.
Now *(*(a+0)+1) = *(1006) here value at address works correctly hence prints 2.
a+0 gives only the base address (i.e) 1002.
*(a+0) also gives the base address (1002) it wouldn't give the value at address (i.e =>1).
*(a+0)+1 => 1002+4 => 1006.
Now *(*(a+0)+1) = *(1006) here value at address works correctly hence prints 2.
(2)
Govind said:
1 decade ago
What would be the output of the following program assuming that the array begins at location 1002 ?
main()
{
int[2][3][4] = {
1,2,3,4,
5,6,7,8,
9,1,1,2
},
{
2,1,4,7,
6,7,8,9,
0,0,0,0
}
};
printf("\n%u%u%u%d",a,*a,**a,***a);
}
main()
{
int[2][3][4] = {
1,2,3,4,
5,6,7,8,
9,1,1,2
},
{
2,1,4,7,
6,7,8,9,
0,0,0,0
}
};
printf("\n%u%u%u%d",a,*a,**a,***a);
}
(1)
Pratik said:
9 years ago
How to know the address when it is not given. Here the &a[0] is 1002. How can you get that?
Mayank said:
1 decade ago
In gcc compiler the code is showing following error:
Warning: format \'%u\' expects argument of type \'unsigned int\', but argument 2 has type \'unsigned int *\' [-Wformat].
Warning: format \'%u\' expects argument of type \'unsigned int\', but argument 2 has type \'unsigned int *\' [-Wformat].
Sriman said:
1 decade ago
s[i]=*(s+i) and,
i[s]=*(s+i). both are same. Only the way of declaration is different.
Similarly,
a[0] can also write as *(a+0). Both are same.
i[s]=*(s+i). both are same. Only the way of declaration is different.
Similarly,
a[0] can also write as *(a+0). Both are same.
R R Beniwal said:
1 decade ago
@Mayank.
a[0]=*a=*(a+0) i.e. all are same.
Here *(a[0]+1) becomes *(*(a+0)+1) that is the value of a[0][1].
If you take example:
int a[3][4] = { 5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
Output will be same.
Don't think it as *(1+1)..think as *(*(a+i)+j) which returns value of a[i][j]... :):).
a[0]=*a=*(a+0) i.e. all are same.
Here *(a[0]+1) becomes *(*(a+0)+1) that is the value of a[0][1].
If you take example:
int a[3][4] = { 5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
Output will be same.
Don't think it as *(1+1)..think as *(*(a+i)+j) which returns value of a[i][j]... :):).
Noor said:
1 decade ago
a[0] is similar to *(a+0) which is *a.
H:) said:
1 decade ago
it is a two dimensional array consists of 3 rows and 4 columns so its like:
1 2 3 4
5 6 7 8
9 10 11 12
But memory is allocated like the two dimensional array is also a general array consists of 4 elements.so if refer a[0]=4, a[1]=5, a[2]=9, a[0]+1=2 like that and we know that memory allocated for array is consecutive blocks.so array starts at 1002 next element is at 1006.
1 2 3 4
5 6 7 8
9 10 11 12
But memory is allocated like the two dimensional array is also a general array consists of 4 elements.so if refer a[0]=4, a[1]=5, a[2]=9, a[0]+1=2 like that and we know that memory allocated for array is consecutive blocks.so array starts at 1002 next element is at 1006.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers