Collection[] public collections;

struct Book {
    string name;
    address writer;
    bytes32 proof;

struct Collection {
    Book[] books;
    address collectionCreator;

// Question: how to push a new collection?
function newCollection() public {
        collectionCreator: msg.sender,
        // ISSUE HERE
        books: <HELP>

How to instantiate this new array of a struct inside a struct?

  • In all honesty, this seems really confusing to implement, you could look at using alternatives, like using a mapping instead.
    – GrandFleet
    Jan 14, 2019 at 4:52

1 Answer 1


I think the easiest thing to do is as follows:

function newCollection() public {
    // Increase the array length first
    collections.length += 1;

    // Get a reference to the new element
    Collection storage collection = collections[collections.length - 1];

    // Set its members as needed        
    collection.collectionCreator = msg.sender;
    // collection.books.push(...);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.