C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 18)
18.
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>
int main()
{
int a[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("%u, %u, %u, %d\n", a, *a, **a, ***a);
return 0;
}
Discussion:
54 comments Page 1 of 6.
Shailesh said:
1 decade ago
No, Chetan is not telling perfectly.
Dear Hari,
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address
These lines are not 100% correct,written by Chetan.
But the fact is that a represents base adds of first block,
*a=base adds of zeroth row &
**a=base adds of zeroth column.
NOw see this carefully,
***a prints 1,The 1 is not the base adds value,(here base adds word is fake b'z there are 3 base addss representing 1 value) but more clearly it is the vale pointed by the base adds of 1st colm or 0th colm here ,& not by pointed by row's or block's base adds ,i think Hari will get this.
again if you wrote like this *(**a),this will print value which is pointed by base adds of column.
So keep in mind clearly that,
a ---- base adds of array represented by block & not by any other.
*a --- base adds of array represented by row & not by any means.
**a ---base adds of array represented by colm & not by any other
*(*a+1)---prints value of 1st row,because *a =base adds of 0th row,but however **a+1 will print value of 1st column, i.e. value 2 in above example.
Dear Hari,
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address
These lines are not 100% correct,written by Chetan.
But the fact is that a represents base adds of first block,
*a=base adds of zeroth row &
**a=base adds of zeroth column.
NOw see this carefully,
***a prints 1,The 1 is not the base adds value,(here base adds word is fake b'z there are 3 base addss representing 1 value) but more clearly it is the vale pointed by the base adds of 1st colm or 0th colm here ,& not by pointed by row's or block's base adds ,i think Hari will get this.
again if you wrote like this *(**a),this will print value which is pointed by base adds of column.
So keep in mind clearly that,
a ---- base adds of array represented by block & not by any other.
*a --- base adds of array represented by row & not by any means.
**a ---base adds of array represented by colm & not by any other
*(*a+1)---prints value of 1st row,because *a =base adds of 0th row,but however **a+1 will print value of 1st column, i.e. value 2 in above example.
Chetan said:
1 decade ago
It depend upon the dimention of the array that how many '*' are needed to get the value.
in case of 1D Array i,e
int arr[]={1,2,3,4};
int *a = arr;
a---- gives the base address and
*a--- gives the value at the base address.
In case of 2d Array i,e
int a[2][3] = {1, 2, 3, 4, 5, 6};
int *a = arr;
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address
Similarly, in case of 3D Array i,e
int a[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} };
int *a = arr;
a----- gives the base address of the array.
*a---- this also gives the base address
**a--- this also gives the base address and
***a--- return the valuue at the base address
Thus the short cut is No OF '*' when it is EQUAL to the Dimention of the array it returns the value.
For 1D --- 1'*'
2D --- 2'*'
3D --- 3'*'
in case of 1D Array i,e
int arr[]={1,2,3,4};
int *a = arr;
a---- gives the base address and
*a--- gives the value at the base address.
In case of 2d Array i,e
int a[2][3] = {1, 2, 3, 4, 5, 6};
int *a = arr;
a----- gives the base address of the array.
*a---- this also gives the base address and
**a--- return the valuue at the base address
Similarly, in case of 3D Array i,e
int a[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} };
int *a = arr;
a----- gives the base address of the array.
*a---- this also gives the base address
**a--- this also gives the base address and
***a--- return the valuue at the base address
Thus the short cut is No OF '*' when it is EQUAL to the Dimention of the array it returns the value.
For 1D --- 1'*'
2D --- 2'*'
3D --- 3'*'
(1)
Rishab said:
4 years ago
@All.
Suppose if you have an array a[10]
And you want to give a[5] = 12.
You can write *(a+i) = 12 (this is nothing but a pointer way of using array)
if you print(a) it will point to the base address of the array.
these are the basics of array.
Now let's work with the question.
if we have 3d array a[10][10][10].
and you want to make a[5][6][7] = 30,
You can use *(*(*(a+5)+6)+7) = 30,
count the stars needed to give the value. 3 stars,
so until 3 stars you will be printing the address
in this case.
"a"
will print address of a
"*a"
will print address of *(a+0) which is same as a
"**a"
will print address of *(*(a+0)+0) which is same as a
but
"***a"
will print VALUE of *(*(*(a+0)+0)+0) which is 1
Hope you all get it.
Suppose if you have an array a[10]
And you want to give a[5] = 12.
You can write *(a+i) = 12 (this is nothing but a pointer way of using array)
if you print(a) it will point to the base address of the array.
these are the basics of array.
Now let's work with the question.
if we have 3d array a[10][10][10].
and you want to make a[5][6][7] = 30,
You can use *(*(*(a+5)+6)+7) = 30,
count the stars needed to give the value. 3 stars,
so until 3 stars you will be printing the address
in this case.
"a"
will print address of a
"*a"
will print address of *(a+0) which is same as a
"**a"
will print address of *(*(a+0)+0) which is same as a
but
"***a"
will print VALUE of *(*(*(a+0)+0)+0) which is 1
Hope you all get it.
(11)
Muhammad Fouad said:
7 years ago
Hey guys, just want to figure out some concepts:-
The array name (without a subscript) is a pointer to the first element in the array.
So, when saying printf( "%d, %d, %d and %d", a, *a, **a, ***a );
The first a is a pointer to the first element, i.e returns the address of the array or the first element of the array, the second a is a pointer to a pointer, also returns am address, the third one also the same, but the last one is exceptional, because the compiler already knows that a 3D array was declared, so a maximum of triple dereferencing statements is obtained, because the forth a which is ***a will return the actual value which is 1!!.
Hope you got it.
The array name (without a subscript) is a pointer to the first element in the array.
So, when saying printf( "%d, %d, %d and %d", a, *a, **a, ***a );
The first a is a pointer to the first element, i.e returns the address of the array or the first element of the array, the second a is a pointer to a pointer, also returns am address, the third one also the same, but the last one is exceptional, because the compiler already knows that a 3D array was declared, so a maximum of triple dereferencing statements is obtained, because the forth a which is ***a will return the actual value which is 1!!.
Hope you got it.
Vishwas said:
1 decade ago
a is a 3d array;
consider a[2][3][4]
subscript 2 represents 2 blocks
subscript 3 represents 3 rows
subscript 4 represents 4 columns
i.e. a is a 3-d array with 2 blocks where each block is a 3*4 matrix
therefore arrangement of elements in a can be visualized as:
a={
block1:
[1 2 3 4
5 6 7 8
9 1 1 2],
block 2:
[2 1 4 7
6 7 8 9
0 0 0 0]
}
1)Now a means/equivalent to &a[0][0][0]
which points to the first block's address which is 1002
2)*a points to first block,first row,which again has address 1002
3)**a points to first block,first row,first col element, address=1002
4)***a is the value stored in first block,first row,first col which is 1
hence we get 1002,1002,1002,1
consider a[2][3][4]
subscript 2 represents 2 blocks
subscript 3 represents 3 rows
subscript 4 represents 4 columns
i.e. a is a 3-d array with 2 blocks where each block is a 3*4 matrix
therefore arrangement of elements in a can be visualized as:
a={
block1:
[1 2 3 4
5 6 7 8
9 1 1 2],
block 2:
[2 1 4 7
6 7 8 9
0 0 0 0]
}
1)Now a means/equivalent to &a[0][0][0]
which points to the first block's address which is 1002
2)*a points to first block,first row,which again has address 1002
3)**a points to first block,first row,first col element, address=1002
4)***a is the value stored in first block,first row,first col which is 1
hence we get 1002,1002,1002,1
Shabnam said:
1 decade ago
a[2][3][4] means two blocks 3 rows and 4 columns.
now
we can write{
{ 1,2,3,4
5,6,7,8
9,1,1,2}//1st block 3 rows 4 columns
{ 2,1,4,7
6,7,8,9
0,0,0,0}//2nd block 3 rows 4 columns
}
now we have given address of array as 1002.so a represents starting address,*a,**a also represents starting address which is 1002.now ***a represents 1st element i.e. 1 that means a[0][0][0]=1 means first block first row and first column.
and after running this program address will be different depending on your computer
now
we can write{
{ 1,2,3,4
5,6,7,8
9,1,1,2}//1st block 3 rows 4 columns
{ 2,1,4,7
6,7,8,9
0,0,0,0}//2nd block 3 rows 4 columns
}
now we have given address of array as 1002.so a represents starting address,*a,**a also represents starting address which is 1002.now ***a represents 1st element i.e. 1 that means a[0][0][0]=1 means first block first row and first column.
and after running this program address will be different depending on your computer
RAJKUMAR said:
1 decade ago
Hi karthi
Here a[2][3][4],1st subscript a[2] explain no of block.means here no of blocks are 2.2nd subscript explain no of rows and 3rd no of columns. here no of rows and columns are 3 and 4.
Example:-
int main()
{
{
1 2 3 4
4 5 6 8 //block 1st,row=3 and columns=4
9 1 1 2
}
{
2 1 4 7
6 7 8 9 //block 2nd
0 0 0 0
}
This is the simplest way of explanation.
I hope you better understand. O K BYE BYE.
Here a[2][3][4],1st subscript a[2] explain no of block.means here no of blocks are 2.2nd subscript explain no of rows and 3rd no of columns. here no of rows and columns are 3 and 4.
Example:-
int main()
{
{
1 2 3 4
4 5 6 8 //block 1st,row=3 and columns=4
9 1 1 2
}
{
2 1 4 7
6 7 8 9 //block 2nd
0 0 0 0
}
This is the simplest way of explanation.
I hope you better understand. O K BYE BYE.
Sangaraj Desai said:
8 years ago
Since the name of the array holds base address of the array hence a=1002
here 1002 something like a value if we use *a then it will return whatever stored in a since a holds 1002
so *a=1002
similarly **a=1002.
For ***a they have mentioned the %d format specifier hence it will return whatever value stored in a[0][0][0]=1..if they specified %u instead of %d once again output will be 1002
hence,
a=1002
*a=1002
**a=1002
***a=1(because of format specifier %d in printf)
here 1002 something like a value if we use *a then it will return whatever stored in a since a holds 1002
so *a=1002
similarly **a=1002.
For ***a they have mentioned the %d format specifier hence it will return whatever value stored in a[0][0][0]=1..if they specified %u instead of %d once again output will be 1002
hence,
a=1002
*a=1002
**a=1002
***a=1(because of format specifier %d in printf)
(1)
Sriram said:
1 decade ago
'a' holds the starting address of 3d array here. So it will be 1002,
In a[][][], the first bracket represents the z-direction view. Second bracket represents rows(X-direction). 3rd brackets represents columns (Y-direction).
*a is nothing but a[][] is the starting address of 2d array in xy-direction.
**a is nothing but a[] is the starting address of 1d array in y-direction.
***a is value at stating of 3d array a
So answers will be: *a=1002, **a=1002, ***a=1.
In a[][][], the first bracket represents the z-direction view. Second bracket represents rows(X-direction). 3rd brackets represents columns (Y-direction).
*a is nothing but a[][] is the starting address of 2d array in xy-direction.
**a is nothing but a[] is the starting address of 1d array in y-direction.
***a is value at stating of 3d array a
So answers will be: *a=1002, **a=1002, ***a=1.
Ajitesh said:
6 years ago
A gives the address of 3d aray.
3d array a[blocks][rows][columns].
*a gives the address of 1st block (z direction) of the array which is same as the address of a.
**a gives the address of 1st row of 1st block of the array which is same as the address of the 1st block.
***a gives value at address specified by **a.
3d array a[blocks][rows][columns].
*a gives the address of 1st block (z direction) of the array which is same as the address of a.
**a gives the address of 1st row of 1st block of the array which is same as the address of the 1st block.
***a gives value at address specified by **a.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers