3

I have following code piece:

  struct PaymentReceipt{
    uint   start_block;
    uint   end_block;
  }
  PaymentReceipt[] paymentReceiptList;

  function verfiyPayment(uint block_start, uint block_end) returns (bool success) {
    paymentReceiptList.push(PaymentReceipt({start_block: block_start, end_block: block_end }));
    return true;
  }

As I know, each new Struct will be pushed on top of the stack, so latest element (paymentReceiptList.length) will always point to the latest pushed Struct.

https://github.com/ethereum/wiki/wiki/Solidity-Features:

Dynamically-sized storage arrays have a member function push, such that var l = arr.push(el); is equivalent to arr[arr.length++] = el; var l = arr.length;.

For example, Java has following approach that: we can insert the specific element at the specified position in the list.

[Q] Is it possible to push an object into an Array with a given index and shift indexes, like the example on Java?

1 Answer 1

2

There is no built-in method for inserting a value at a specific position in an array. You'll just have to write a loop to do it.

If you're going to be inserting a lot of elements in the middle of the array, it may make more sense to use a linked list instead of an array, so that you aren't doing as many storage writes, which can be expensive.

2
  • I am thinking to implement LinkedList as the implementation in C as via pointer. In order to do that I need to allocate memory space for node and its address. I was not able to obtain its address. struct node{ uint data; uint next; } node head = node(0,0); is it possible to get head's address in the memory?
    – alper
    Jan 29, 2017 at 13:09
  • 1
    I'm not exactly sure what you're asking. For a linked list, you need to keep a pointer to the head separate from the rest of the data structure. Jan 29, 2017 at 18:12

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.