Lets take a look at a somewhat smaller array of arrays look like in memory:
char a[3][3];
This will be something like
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] | a[2][0] | a[2][1] | a[2][2] |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
A call like memset(ans,'$',sizeof(ans[0][0])*2*2);
will set the memory for the four first memory locations. That is with our example above it will be a[0][0]
, a[0][1]
, a[0][2]
and a[1][0]
. The memory for e..g a[2][1]
will not be touched.
For your array, ans[0][0]
is the first element, followed by ans[0][1]
, and all the remaining 999 elements of a[0]
before a[1][0]
.
That leads to the elements that will be set by your memset
call will be ans[0][0]
, ans[0][1]
, ans[0][2]
and ans[0][3]
. The rest of the memory for ans
will be all zero.
And since you print using one-based indexes, you print ans[1][1]
, ans[1][2]
, ans[2][1]
and ans[2][2]
. Which are all zero.
Oh I see that you updated your question, which means you will print ans[0][0]
, ans[0][1]
, ans[1][0]
and ans[1][1]
. The first two characters should be '$'
, while the second two will still be zero.