4

[Q] I was wondering is it possible to get an address of a struct on the memory space? Inside initialise() function after the Node object creation, I want to get is address if it is possible. Since between contracts I can only transfer its address value.

Thank you for your valuable time and help.

library LinkedList {
    uint80 constant None = uint80(0); /// NULL value.                                                           

    struct data {
        Node head;
        Node[] node_array;
    }

    struct Node {
        string data;
        uint80 prev;
        uint80 next;
    }

    function initialize(data storage self){
        self.head = Node({prev: None, next: None, data: "",});
        self.head.next = /// self.head's address.        
    }        
}

Inside initialize() following code piece works:

Node s = self.node_array[index];
uint addr;
assembly {
    addr := s
}

Inside initialize() the following code piece does not work:

Error: Expected elementary inline assembly operation.

self.head = Node({prev: None, next: None, data: 10,});

uint addr;
assembly {
    addr := self.head   
}

or

Node new_node;
new_node.data = d;
new_node.next = h.next;
new_node.prev = h.own;

uint newnode_addr;
assembly {
   newnode_addr := new_node
}

newnode_addr returns 0 instead of a valid address.

I am not able to do following assignment also:

Error: Type struct Node memory is not implicitly convertible to expected type struct Node storage pointer.

Node newnode = Node({prev: None, next: None, data: 10,});
1

Why are you trying to get a pointer to the struct? Are you trying to get a copy of that struct into another contract? Is this what you're meaning by between contracts I can only transfer its address value?

You can write constant functions which can query the state of a given contract, and will easily allow you to expose the node memory to other contracts. For example getDataForNode(i) which can return the data field of a given node at index i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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