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;
}
1002, 2004, 4008, 2
2004, 4008, 8016, 1
1002, 1002, 1002, 1
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
54 comments Page 4 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.

Hari said:   1 decade ago
Thanks chetan done a nice job.

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'*'
(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

Vishal said:   1 decade ago
@uttam Raj
This is assume that array begins from location 1002 but actualy it different.

Uttam raj said:   1 decade ago
I didn't the answer but when i execute the is completely different from the options..... when i execute the answer i got is
(3209883548, 3209883548, 3209883548, 1).....how this is possible

Suresh said:   1 decade ago
How the value 1 will come ?

In that values which 1 should take?

I did'nt under stand?

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.

Hameetha said:   1 decade ago
Thanks sriram for clear explanation.

Sneha said:   1 decade ago
@sriram good explanation. It cleared my doubt.


Post your comments here:

Your comments will be displayed after verification.