Skip to content

2.4. Structs#

Syntax#


StructDefinition = (* struct_keyword: *) STRUCT_KEYWORD
(* name: *) IDENTIFIER
(* open_brace: *) OPEN_BRACE
(* members: *) StructMembers
(* close_brace: *) CLOSE_BRACE;

StructMembers = (* item: *) StructMember*;

StructMember = (* type_name: *) TypeName
(* name: *) IDENTIFIER
(* semicolon: *) SEMICOLON;

Struct Types#

Structs are custom defined types that can group several variables. They can be defined inside or outside contracts.

struct Voter {
    address delegate;
    uint vote;
}

You can also create new objects of this struct using the following syntax:

contract MyContract {
    function create() public  {
        Voter memory v = Voter({
            delegate: msg.sender,
            vote: 1
        });
    }
}

Note

This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.