How do I initialize an array in a struct

0 votes

I have a struct Purchase in which I'm putting an array of payments. However, when I try to add the new payments array in my makePaymentmethod I get an error back from the solidity compiler: "Internal compiler error: Copying of type struct Payment memory[] memory to storage not yet supported." When I change the mayment array to be storage or memory, I get the same error. I've added the relevant code below.

Is it possible to do what I'm trying to do in solidity? I don't see anything explicitly saying it's not possible in the documentation but I also don't see any examples doing what I'm trying to do. :|

  struct Payment {
    address maker;
    uint amount;
  }

  struct Purchase {
    uint product_id;
    bool complete;
    Payment[] payments;
  }
  Purchase[] purchases;

  function makePayment(uint product_id, uint amt, uint purchase_id) returns (bool) {

      Payment[] payments;
      payments[0] = Payment(address, amt);
      purchases[purchase_id] = Purchase(product_id, false, payments);
  }
Oct 1, 2018 in Blockchain by sabby
• 4,370 points

retagged Nov 22, 2018 by Priyaj 3,011 views

1 answer to this question.

0 votes

You need to manually change the length of the payments array when setting it.

Either use:

  Payment[] payments;
  payments[payments.length++] = Payment(address, amt);

Or:

Payment[] payments;
payments.push(Payment(address, amt));

For setting the payments array in Purchase, instead of creating an array and trying to set it to the Purchase.payments you can do the following:

uint purchase_id = purchases.length++;
purchases[purchase_id].product_id = product_id;
purchases[purchase_id].complete   = false;
purchases[purchase_id].payments.push(Payment(msg.sender, amt));

Extending the purchases length will automatically create the new attributes. Then you can set them manually.

answered Oct 1, 2018 by Perry
• 17,100 points

Related Questions In Blockchain

+4 votes
2 answers

How do I create a new block in Hyperledger Fabric?

This link might help you: https://github.com/hyperledger/fabric-sample ...READ MORE

answered Oct 11, 2018 in Blockchain by Sahu
2,655 views
0 votes
1 answer
+2 votes
5 answers

How do I parse this JSON array in PHP?

/** * Firstly collect the ...READ MORE

answered Sep 3, 2018 in Blockchain by slayer
• 29,370 points
6,333 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,464 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,601 views
0 votes
1 answer

How do I get the balance of an account in Ethereum?

On the Web: (Not programmatic, but for completeness...) ...READ MORE

answered Nov 15, 2018 in Blockchain by Perry
• 17,100 points
17,255 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP