I had a question. I have a mapping that maps to struct.

  contract A{

    struct struct1{

    struct struct2{
      struct1 input1;

    mapping (uint => struct1) mapToStruct;

    struct2[] allEntries;
      //...rest of the code for populating mapToStruct        

  contract B is A{

    function addToStruct2(uint _mappingNumber) returns(bool status){
      struct2 memory newMember;
      struct1 memory newEntry;
      newEntry = mapToStruct[_mappingNumber];
      newMember.input1 = newEntry;
      return true;

But this code is not working, I am not able to add the details to allEntries. The array has a new entry but all its members are 0, even after addToStruct2 returns true. Thanks in advance


I got fairly tangled up when I first started debugging this. The nested structs seem unnecessarily busy. I would incline toward a well-solved pattern. Have a look at some basic patterns over here: Are there well-solved and simple storage patterns for Solidity?

I think you can use "mapping with structs", "array of structs with unique Id" or even "mapping with structs and delete" - a sort of swiss army knife pattern for a lot of cases.

Hope it helps.


This will let you send a key and two inputs and store the data. You can get a key count and fetch the keys by number. You can fetch the stoed inputs using a valid key. When you fetch an unknown key, isSet will be false.

contract Simple {

  struct MyStruct {
    uint input1;
    uint input2;
    bool isSet;

  mapping(bytes32 => MyStruct) public myStructs;
  bytes32[] public keys;

  function addToData(bytes32 key, uint input1, uint input2)
    returns(bool success)
    myStructs[key].input1 = input1;
    myStructs[key].input2 = input2;
    myStructs[key].isSet  = true;
    return true;

  function getKeyCount()
    returns(uint keyCount)
    return keys.length;

  • This is interesting! I would surely try to change the code after reading this. But is there any specific reason that I was getting an blank entries. Firstly I thought it was because I was using data from another contract, then to remove this problem, I created a function, returned all the values I wanted to store in struck2 and saved it in a tuple. and then to initilize it i used struct2 tempStruct = struct2(x,y,z,...); and then allEntries.push(tempStruct);, but still no progress. I understand it is getting very complex, but there must be something which I missing. btw Thanks again :) – 11t Mar 28 '17 at 21:51
  • Near as I can tell, mapToStruct is a mapping that points to structs with no members (struct1). No value is ever set anywhere. Does struct2.input1 ever get set? Also, don't put everything inside everything else. Suggest you also choose a place for the struct(s) to live and use key/row pointers to maintain other indexes/lists as you require. – Rob Hitchens Mar 29 '17 at 1:28
  • answering your 1st question on mapping that points to structs with no members, in the contract in get populated but I did not think it was not necessary for my question (apologies if it caused confusion) and 2nd "struct2.input1 ever get set" I tried it but the problem which I was facing was all the elements were zero. I have learned my lessons and your answer was very useful. Thanks a lot – 11t Mar 29 '17 at 7:32

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.