Cannot set the values of struct variable in solidity
when i am trying to initialize the values for array of struct variable i am not able do it
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.11;
contract shop{
struct student{
string name;
uint roll;
}
student[] public s;
function setstudent() public{
s[0].name = "sam";
s[0].roll = 98246;
}
}
Solution 1:
You're trying to assign into a non-existing index of the array.
For creating a new item (i.e. increasing the array size), use the push()
function.
s.push(
student("sam", 98246)
);