Doubt in 2D array

Hello, suppose I have a 2d array

a [9] [3] = { 0 } ;

and let’s say I want to print its 3rd row then if I write

cout<<a[2] <<endl;

will it print my 3rd row ?

Hey @yashsharma4304
No it wont ,instead it will print the starting address of that row
When you do *a[2] it will print element at a[2][0]

So what is happening in this case here ?

Hey @yashsharma4304
Things are different in char arrays
It will start printing from that location and keeps printing until they find a NULL, if u remove β€˜\0’ after a,b and then print a[0] it will print a,b,(garbage char),d,e,f

Ok so in this code what I am getting in the output? Is it a garbage value or something else

Bro you are using integer arrays in above code so

Ok so 0x7ffd8f6dd260 is representing the address of the first block of that array ?

Yes
…

1 Like

I have removed the null character in my code but why is it not printing garbage value ? Instead of printing garbage value it is printing the value inside next row.

because there are 4 columns and all are filled,
Do this in same program : char b[3][10] = {{β€˜c’,β€˜d’,β€˜e’,β€˜f’}, {β€˜y’,’\0’},{β€˜d’,β€˜e’,’\0’}}; make cols 10
Sometimes garbage can also contain β€˜\0’ chars

1 Like

Sometimes garbage can also contain β€˜\0’ chars

Ok.