Skip to main content
2 of 2
deleted 15 characters in body; edited title
Rob Hitchens
  • 55.6k
  • 11
  • 91
  • 146

Returning dyanamic array from function

I need to access books array of Student contract in ClassRoom contract.

If i write as function readStudentStruct (uint ID) constant returns (address, uint, bool,address[]) then error

Different number of arguments in return statement than in returns declaration.
        return student.StudentNames(ID);

or if written without address[] then works fine.

pragma solidity ^0.4.0;
contract Student{
    
    struct stu{
        address name;
        uint age;
        bool tookTest;
        address[] books;
    }
    
    stu public s;
    
    mapping(uint => stu) public StudentNames;
    
    function addStudent (uint ID, address _name, uint _age,address[] books) {
        StudentNames[ID] = stu(_name, _age, false, books);
    }
    function updateStudent (uint ID) {
        StudentNames[ID].tookTest = true;
    }
    
    function getBooks(uint ID) returns(address[]){
        return StudentNames[ID].books;
    }
    
}
contract ClassRoom {
    address studentAddr;
    
    Student student;
    
    function ClassRoom(address addr) {
        studentAddr = addr;
        student = Student(addr);
    }
    function updateTookTest (uint ID) {
        student.updateStudent(ID);
    }
    
    //working fine
    function readStudentStruct (uint ID) constant returns (address, uint, bool) {
        return student.StudentNames(ID);
    }

   //showing error
   function readStudentStruct (uint ID) constant returns (address, uint, bool,address[]) {
        return student.StudentNames(ID);
    }
}

as per struct with address array function should worked but didn't.

why books array not coming in parent contract? or any way to get books array in parent contract?

comeback4you
  • 302
  • 1
  • 3
  • 11