Solidity has the concept of reference types and there are several of them such as dynamic arrays, fixed arrays, mappings and structs. You can read more about that here:
https://solidity.readthedocs.io/en/v0.4.24/types.html
A struct is like a class of sorts where we have to create a new instance of it to use it.
This struct will have a couple of different fields that will describe its purpose for example:
contract Crowdfunding {
struct Request {
string description;
uint value;
address recipient;
bool complete;
}
}
Now a struct behaves differently than variables do, for example:
contract Crowdfunding {
struct Request {
string description;
uint value;
address recipient;
bool complete;
}
address public manager;
uint public minimumContribution;
address[] public approvers;
function Crowdfunding(uint minimum) public {
manager = msg.sender;
minimumContribution = minimum;
}
}
Below the struct I have variables I created that I can access in a function, but a struct does not contain variables it contains reference types. So if you want to use the Request
struct, then you have to create a new variable and say it is a type of Request
.
So the above does not create an instance of a Request
it just creates the idea of it or the type.
To actually create a request and have it stored inside the contract I will make an array that specifically holds variables of type request.
I am going to make a new array and specify its type is being request.
contract Crowdfunding {
struct Request {
string description;
uint value;
address recipient;
bool complete;
}
Request[] public requests;
address public manager;
uint public minimumContribution;
address[] public approvers;
function Crowdfunding(uint minimum) public {
manager = msg.sender;
minimumContribution = minimum;
}
Notice I am using capital R because I used a capital R to define the struct. By convention we use capital for first letter when defining a struct which is dissimilar to how we do our other type definitions.
This variable of type Request
is given the name requests
and now anywhere in my contract I can make use of the requests
array.
I could say requests.push()
and push in a new request or give me the new requests in there with request index of zero like so: requests[0]
.
Info b = t[1];
is implicitly the same asInfo storage b = t[1];
, sob
is actually a pointer to the struct in storage.