# Struct in System verilog

**URL:** <https://verificationacademy.com/forums/t/struct-in-system-verilog/37599>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-arrays-struct-constraint-randomization-indexes\
**Created:** [May 18, 2020, 5:48pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599 "2020-05-18T17:48:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Design\_Engineer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@Design\_Engineer](https://verificationacademy.com/forums/u/Design_Engineer)\
**Post date:** [May 18, 2020, 5:48pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/1 "2020-05-18T17:48:04Z")

</div>

How to declare an array of size 10 in structure?

Actually i need to create a student database for 10 students,with their age,name,class,status using struct

My question is how to declare the values for the input?

here is my code

```verilog
typedef struct{
    int age;
string name;
byte class;
bit status;
}student_data;

student_data base='{10,"John",2,1} //This is for a single student

```

How to do the same for 10 students?

---

<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:** [May 18, 2020, 6:26pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/2 "2020-05-18T18:26:01Z")

</div>

_In reply to [Design Engineer](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599):_

You can turn base into a dynamic array by adding then using the array concatenation syntax.

```verilog
student_data base[];

base ={ '{10,"John",2,1}, '{11,"Paul",2,0}, '{12,"George",2,0}, '{13,"Ringo",2,0}, ... } ;

```

---

<div class="post-metadata">

**Author:** ![juhi\_p](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@juhi\_p](https://verificationacademy.com/forums/u/juhi_p)\
**Post date:** [May 18, 2020, 6:47pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/3 "2020-05-18T18:47:15Z")

</div>

you can also simply create array of 10 elements like

```auto
student_data base[10]

```

and allocate them using foreach loop or using array concatenation as answered above.

---

<div class="post-metadata">

**Author:** ![Design\_Engineer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@Design\_Engineer](https://verificationacademy.com/forums/u/Design_Engineer)\
**Post date:** [May 18, 2020, 6:54pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/4 "2020-05-18T18:54:14Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/3):_

I got the error as

ERROR VCP5211 “Aggregate literal is not allowed in this context.” “testbench.sv” 11 80

Here is the code

module tb;  
typedef struct{  
int age;  
string name;  
byte classes;  
bit status;  
}student\_data;

student\_data base;  
initial begin  
base ={‘{10,“John”,2,1},’{11,“Paul”,2,0},'{12,“George”,2,0}, '{13,“Ringo”,2,0}};  
$display(“student details=%p”,base);  
end  
endmodule

---

<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:** [May 18, 2020, 8:09pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/5 "2020-05-18T20:09:26Z")

</div>

_In reply to [Design Engineer](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/4):_  
Sorry, forgot that the array concatenation syntax does not allow the nested implicit assignment pattern. You would have to write

```verilog
  base ={student_data'{10,"John",2,1},student_data'{11,"Paul",2,0},student_data'{12,"George",2,0}, student_data'{13,"Ringo",2,0}};

```

But you can also just change the outer concat into an assignment pattern

```verilog
base ='{'{10,"John",2,1},'{11,"Paul",2,0},'{12,"George",2,0}, '{13,"Ringo",2,0}};

```

---

<div class="post-metadata">

**Author:** ![Design\_Engineer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@Design\_Engineer](https://verificationacademy.com/forums/u/Design_Engineer)\
**Post date:** [May 18, 2020, 8:49pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/6 "2020-05-18T20:49:26Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/5):_

I have got the exact output

Thanks a lot for your timely support @dave

As I am new to SV,can you suggest me some ideas to improve my programming skills?

---

<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:** [May 18, 2020, 9:01pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/7 "2020-05-18T21:01:49Z")

</div>

_In reply to [Design Engineer](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/6):_

> **[EDA Software, Hardware & Tools](https://eda.sw.siemens.com/en-US/)**
>
> Siemens EDA delivers the world’s most comprehensive portfolio of electronic design automation (EDA) software, hardware and services.

And other blog posts [here](https://blogs.mentor.com/verificationhorizons/).

---

<div class="post-metadata">

**Author:** ![Design\_Engineer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@Design\_Engineer](https://verificationacademy.com/forums/u/Design_Engineer)\
**Post date:** [May 18, 2020, 9:52pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/8 "2020-05-18T21:52:50Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/7):_

Thanks a lot @ dave

And one more doubt in the same question,

How to sort the array based on the age of students?

And how to find the student whose age is more than 10?

---

<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:** [May 18, 2020, 10:17pm UTC](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/9 "2020-05-18T22:17:55Z")

</div>

_In reply to [Design Engineer](https://verificationacademy.com/forums/t/struct-in-system-verilog/37599/8):_

See Section _7.12 Array manipulation methods_ in the 1800-2017 LRM.
