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 4 of 7.
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)
Shrinivas Patgar said:
1 decade ago
In 1D array int a[5] = {1,2,3}; a[0] is equal to *(a+0) is same as *(0+a) is same as 0[a] whose value is 1.
In 2D array int a[2][2] = {1,2,3}; a[0][0] is equal to *(a[0]+0) is same as *(*(a+0)+0) whose value is 1.
In 2D array int a[2][2] = {1,2,3}; a[0][0] is equal to *(a[0]+0) is same as *(*(a+0)+0) whose value is 1.
Ayesha said:
1 decade ago
Is (a+0) and a[0] is same.
Does *(*(a+0)+1) becomes *(1+1).
Does *(*(a+0)+1) becomes *(1+1).
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.
Shashi said:
1 decade ago
First let me clarify some basic things.
1. a[0]==*(a+0).
2. a[0]+1==[a[0]address+next byte(int in this case) address].for example
a[0]=500 and int will take 4 bytes then a[0]+1==500+4=504 is same as[(a+0)+1].
3. *(a[0]+1)== value at(504)==*(*(a+0)+1).
4. *(*(a+0)+1)==value at(3).
1. a[0]==*(a+0).
2. a[0]+1==[a[0]address+next byte(int in this case) address].for example
a[0]=500 and int will take 4 bytes then a[0]+1==500+4=504 is same as[(a+0)+1].
3. *(a[0]+1)== value at(504)==*(*(a+0)+1).
4. *(*(a+0)+1)==value at(3).
Abc said:
1 decade ago
a[0] equals *(a+0).
a[0][0] equals*(*(a+0)+0).
So in general a[I][J] == *(*(a+I)+J).
a[0][0] equals*(*(a+0)+0).
So in general a[I][J] == *(*(a+I)+J).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers