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
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?
In reply to Design Engineer:
You can turn base into a dynamic array by adding then using the array concatenation syntax.
student_data base[];
base ={ '{10,"John",2,1}, '{11,"Paul",2,0}, '{12,"George",2,0}, '{13,"Ringo",2,0}, ... } ;
you can also simply create array of 10 elements like
student_data base[10]
and allocate them using foreach loop or using array concatenation as answered above.
In reply to dave_59:
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
In reply to Design Engineer:
Sorry, forgot that the array concatenation syntax does not allow the nested implicit assignment pattern. You would have to write
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
base ='{'{10,"John",2,1},'{11,"Paul",2,0},'{12,"George",2,0}, '{13,"Ringo",2,0}};
In reply to dave_59:
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?
In reply to Design Engineer:
And other blog posts here.
In reply to dave_59:
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?
In reply to Design Engineer:
See Section 7.12 Array manipulation methods in the 1800-2017 LRM.