# Queue of dynamic arrays

**URL:** <https://verificationacademy.com/forums/t/queue-of-dynamic-arrays/46024>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [January 31, 2024, 4:36am UTC](https://verificationacademy.com/forums/t/queue-of-dynamic-arrays/46024 "2024-01-31T04:36:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshitijraj](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kshitijraj](https://verificationacademy.com/forums/u/kshitijraj)\
**Post date:** [January 31, 2024, 4:36am UTC](https://verificationacademy.com/forums/t/queue-of-dynamic-arrays/46024/1 "2024-01-31T04:36:25Z")

</div>

I am trying to create a queue of dynamic arrays. See the code below:

```auto
module tb;
  typedef int packet_item[]; // dynamic array as a data type
  packet_item packet_queue[$]; // queue of dynamic array elements
  
  initial begin
    packet_item ten = '{10,10};
    packet_queue.push_back(ten);
    packet_item eleven = '{11,11};
    packet_queue.push_back(eleven);
    foreach(packet_queue[i]) begin
      $display("Index: %0d -- Elements: %0p",i, packet_queue[i]);
    end
  end
endmodule

```

This does not work at line packet\_item eleven = '{11,11}; throwing a syntax error. However. if I move packet\_queue.push\_back(ten); after creating the array eleven, it works perfectly fine. See the modified code below:

```auto
module tb;
  typedef int packet_item[]; // dynamic array as a data type
  packet_item packet_queue[$]; // queue of dynamic array elements
  initial begin
    packet_item ten = '{10,10};
    packet_item eleven = '{11,11};
    packet_queue.push_back(ten);
    packet_queue.push_back(eleven);
    
    foreach(packet_queue[i]) begin
      $display("Index: %0d -- Elements: %0p",i, packet_queue[i]);
    end
  end
endmodule

```

Can someone explain this behavior? Thank you

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 31, 2024, 6:34am UTC](https://verificationacademy.com/forums/t/queue-of-dynamic-arrays/46024/2 "2024-01-31T06:34:12Z")

</div>

Your problem has to do with the placement of variable declarations, nothing to do with arrays. Because of the way Verilog defines the syntax for a `begin/end` blocks, variable declarations must come before any other statements. In your original example, `eleven` is declared after a function call and becomes a syntax error.

Your modified code is also illegal, but most tools just throw a warning. Best to move the variable declaration outside the initial block. See [Function arguments not initializing variable inside the body](https://verificationacademy.com/forums/t/function-arguments-not-initializing-variable-inside-the-body/31889)
