Question related to array

Hi All,


int a;
bit [5:0] ss [3][3];
initial begin
  foreach(ss[i]) 
      foreach(ss[i,j])
       ss[i][j] = a++;
end

Why is the value of ss {18 19 20} {21 22 23} {24 25 26}?
I supposed it should be {0 1 2} {3 4 5} {6 7 8}
Thanks!

In reply to peter:

As you have outer foreach loop you observe :: ss = {18 19 20} {21 22 23} {24 25 26}

The output would be as per your expectation if you were to remove the outer foreach loop i.e


 initial begin
      foreach(ss[i,j])
       ss[i][j] = a++;
end

Try adding the following display


initial begin
  foreach(ss[i]) //  Iterates  Thrice 
   begin
      $display("Iterating  for  i ==  %0d  in  Outer  foreach " , i );
      foreach(ss[i,j]) //  Iterates  :: 9 x 3  i.e  27  times . Latter  values  override 
       begin
         ss[i][j] = a++;
       end
   end
end