[Ethereum] Working with structure arrays in solidity


I have the following contract code example:

contract C {

    struct A {
        uint256 a;
        uint256 b;

    struct B {
        A ax;
        uint256 c;

    struct C {
        string name;
        B[] bs;

    function init() {
        C memory c;
        B memory b;
        A memory a;

        a.a = 10;
        a.b = 20;

        b.ax = a;
        b.c = 30;

        c.name = "Test test test";
        c.bs[0] = b;

Why can't I use push for the last statement

 c.bs[0] = b;



It throws the following error:

Untitled:30:9: Error: Member "push" is not available in struct B memory[] memory outside of storage.

but bs is an array of structures.

Best Answer

Push is available only on storage arrays, that is member/ state variables and not in memory arrays, that is local variables:

push: Dynamic storage arrays and bytes (not string) have a member function called push that can be used to append an element at the end of the array. The function returns the new length.