in Programming in C
1,431 views
3 votes
3 votes
main( )
{
int n[3][3] = {
2, 4, 3,
6, 8, 5,
3, 5, 1
} ;
printf ( "\n%d %d %d", *n, n[3][3], n[2][2] ) ;
}
in Programming in C
1.4k views

15 Comments

garbage , 0 , 1.
0
0
Why 1st o/p is garbage? , it must return the 1st element of array right?
1
1
Why is n[3][3]=0??
0
0
n[3][3] must be a garbage value
0
0
No @Parshu because its a 2d array and to dereference the 1st element you need ** not just *
1
1
Type of n is int (*)[3].

Type of *n is int (*).

So *n will be pointing to the first element which is 2. It will print the address of 2.

A[3][3] will print the garbage value.

A[2][2] print the last element which is 1 here.
4
4
@anu , here n[3][3] is out of the range of array mentioned.

n[3][3] has n[0][0].....n[2][2] total 9 elements

hence n[3][3] is grabage
1
1

@anu, here array n is a pointer but it will be pointing to the first element which is an array of 3 elements.

See this question, https://gateoverflow.in/136596/pointers

Also, here we explicitly assign values to the array. So it will not consist all of its element as 0.

1
1
@Anu sir,

what you are thinking garbage value is actually address of n[0]
0
0
nitish yes address is printed but since %d is given not %u so i take it as garbage,
0
0
as long as MSB bit=0 of address, %d will also give correct answer, but it will give wrong value if MSB=1.
1
1
how we know msb bit is 0.? bdw yes it print address,
0
0

@Hemant 

the first element is " *n " so it should give the value of the 1st element of the array. If only " n " will be there then it will print its address! 

Tell me If I am wrong! 

Thanks in advance.

0
0

I am getting garbage, garbage, 1.

0
0
remember this when we say 'n' it is '&n'

'n[i]' is equivalent to '  *(n +i ) '     reference : yashavant kanetkar

in the case of 1D array if we say *n  -> *(n+0)  -> n[0] which is an integer then we can get the value

 in that place

in the case of 2D array if we say *n -> n[0] in the case of 2D array it is still an array so we get the

 adress of the first 1D array so we'll dereference it again this time we get the value of the integer

 stored in [0][0]

i don't know to what extent i am correct

the main observation i observed is if n[i] is an integer we can get that

if n[i] is an array we get the adress of the array
0
0

1 Answer

0 votes
0 votes
2, garbage, 1

2 Comments

it is *n, which will return address,

if it would be **n, then the first value printed would be 2

0
0
Ohk. Got it! Thank you :)
0
0

Related questions