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

黃智祥
  • 465
  • 6
  • 16

1 Answers1

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.

max taldykin
  • 2,966
  • 19
  • 27