2
contract old{
    struct Student{
        bytes32 name;
        uint age;
    }
    uint public sno = 0;
    mapping (uint => Student) pupil;
    function input(bytes32 n, uint a){
        <Student updated>
    }
    function output() constant returns(bytes32[], uint[], uint){
        bytes32[] memory n = new bytes32[](sno);
        uint[] memory a = new uint[](sno);
        for(uint i=0; i<sno; i++){
            Student s = pupil[i+1];
            n[i] = s.name;
            a[i] = s.age;
        }
        return(n,a,sno);
    }
}


contract receiver{
    old o;
    struct Student1{
        bytes32 name1;
        uint age1;
    }
    uint public sno1 = 0;
    mapping (uint => Student) pupil;
    function set(){
      ........
    }
}

Want to copy struct in contract old to struct in contract receiver. I know struct can not be acceseed directly in another contract but want to do it through array.

3
  • 1
    Can you simply this as short and crisp as possible and describe expected versus actual outcome? Might be easier to help you out. Thanks. Jan 4 '17 at 18:08
  • This is a lot of unnecessary code to dig through. What you should do is write an example that contains only the code you are struggling with, with one function for each problem. Jan 4 '17 at 19:13
  • I tried to make the code clean. If possible go through it stuck on this problem from few days. Jan 5 '17 at 8:03
2

There's way too much going on here. As general guidance, I'd avoid for loops as much as possible in Solidity. It's often a red flag.

Here's some code that compiles. Have a look at the the way receiver refers to old with reference to its known address on the blockchain. You can solve it a different way but it's important to solve it.

pragma solidity ^0.4.6;

contract Old {

  struct Thing {
    uint x;
    uint y;
  }

  Receiver r;

  function Old() {
    // When you deploy Old, you also deploy Receiver. See comment near line 25.
    r = new Receiver();
  }

  // 10 items per list is arbitrary size. It makes the interface static in size.
  // You can only pass dynamic sized elements between functions in the same contract.
  function output() returns(bytes32[10] n, uint[10] a, uint sno, uint x, uint y) {
    // do stuff

    Thing memory thing;
    thing.x = 1;
    thing.y = 2;
    return(n,a,sno,thing.x, thing.y); 
  }

  function getReceiver() returns(address receiver) {
    return r;
  }
}

contract Receiver {

  struct Thing {
    uint x;
    uint y;
  }

  address old;

  function Receiver() {
    old = msg.sender;
  }

  function pull() returns(bytes32[10] en, uint[10] ay, uint snow, uint ex, uint why) {
    Old o = Old(old); // needs to the address of "Old" somehow.
    Thing memory thing;
    bytes32[10] memory n;
    uint[10] memory a;
    uint sno;
    (n, a, sno, thing.x, thing.y) = o.output();
    return (n,a,sno,thing.x, thing.y);
    }
  }
2
  • 1. When calling pull function from contract receiver VM Exception: invalid JUMP at....... using solidity browser compiler Jan 5 '17 at 12:46
  • I added a function in Old{} to expose the Receiver contract created address. When using browser solidity, use Receiver At Address (address returned from Old()). Also added return vals from pull(). Hope it helps. Jan 20 '17 at 19:03

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.