1

How do I initialise empty arrays and structs in nested structs data structures?

contract NewBook {

        struct ReviewerData {
            string name,
            uint industryXP
            // foobar...
        }

        struct ChapterData {
            // string chapterID;
            string title;
            string color;
            uint pages;
            //How do I initialise the two elements below with empty values?
            ReviewerData[] reviewer;
            address[] reviewers;
        }

        struct BookData {
            // string bookID;
            ChapterData[] chapters;
        }

        mapping(bytes32 => BookData) books; // bookId => BookData

        function addChapter(bytes32 _bookID, string memory title, string color, uint pages) public {
            ChapterData memory c = ChapterData({
                title: title,
                color: color,
                pages: pages,
                //how to initialise empty reviewer struct?
                //how to initialise empty reviewers array?
            });
            books[_bookID].chapters.push(c);
        }
0

From the Solidity documentation on dynamic arrays:

Arrays have a length member that contains their number of elements. The length of memory arrays is fixed (but dynamic, i.e. it can depend on runtime parameters) once they are created. For dynamically-sized arrays (only available for storage), this member can be assigned to resize the array.

and on the lack of null values:

The concept of “undefined” or “null” values does not exist in Solidity, but newly declared variables always have a default value dependent on its type. To handle any unexpected values, you should use the revert function to revert the whole transaction, or return a tuple with a second bool value denoting success.

In your example - instead of creating the new struct in memory and then using push to append it to the storage array, you can increase the length of books[_bookID].chapters which will initialize a new, empty ChapterData struct. Then edit this new storage struct in place.

    function addChapter(bytes32 _bookID, string memory title, string color, uint pages) public {
        uint _length = books[_bookID].chapters.length;
        books[_bookID].chapters.length++;

        ChapterData storage c = books[_bookID].chapters[_length];
        c.title = title;
        c.color = color;
        c.pages = pages;

    }
  • I understand that these types will be initialised with a 0 value but you cannot just ignore elements of a struct, they must be strictly declared and initialised with a value when you attempt to add a struct to another struct. – NowsyMe Jul 14 at 14:41
  • sorry, i misunderstood the question a bit. I've edited my answer, I hope it's more helpful. – iamdefinitelyahuman Jul 14 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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