2

Sir,

When I execute assignment statement in order to insert/update int value to one cell in 2-dimensional array,it doesn't succeed. The geth console didn't response any error message.The contract just return 0.

My contract is like below:

pragma solidity ^0.4.2;
contract MemDeposit
{
   struct deposit {
   string[] ss_ids;  
   int[][] si_deposit;
}
mapping(address=>deposit) private si_md;  
.......

function addMemDeposit(address p_memacct,string p_acctstr,uint p_idx2,int p_money) external returns(int) {
   int li_idx1;
   li_idx1 = 0 ; //just for test
   ...
   si_md[p_memacct].si_deposit[p_idx2][li_idx1] = p_money; //this doesn't work,contract quit here,client get 0
   ...

   }

}   

I have debug this contract,the error occurs in the assign statement.So my question is how to insert(or update) data to the cell in 2-dimensional array?

Sincerely!

Barkely

1 Answer 1

1

You need to initialize your array before updating values in it.

When you call addMemDeposit(p_memacct, ...) with fresh p_memmacct, this code:

si_md[p_memacct].si_deposit

implicitly creates a new instance of deposit struct. And this instance is empty. This means that ss_ids is an empty array and si_deposit is an empty array.

But in this line, you are trying to update nonexistent value in an empty array:

si_md[p_memacct].si_deposit[p_idx2][li_idx1] = p_money;

Try to init si_deposit before updating it. For example:

function initMemDeposit(address p_memacct) {
    si_md[p_memacct].si_deposit = [[int(1), 2, 3]];
}

Check this question for more info on two-dimensional arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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