Hot answers tagged

83 votes
Accepted

How to Initialize a Struct?

Yes, just use fooStruct myStruct = fooStruct(1,2); Or fooStruct myStruct = fooStruct({foo:1, fighter:2}); Or fooStruct memory myStruct; // for temporary data myStruct.figther = 2; // will only ...
user avatar
  • 34.5k
48 votes
Accepted

Returning a struct and reading via Web3

Summary Return the fields of the struct as separate return variables. Edit: As of 2021, it's possible to return the struct directly. See this answer. Example I'm running this code in my local dev ...
user avatar
43 votes
Accepted

How can I return struct when function is called?

You can not return a struct because Solidity implements them only as a loose bag of variables, they are not real objects. You can use a solution from this answer: https://ethereum.stackexchange.com/a/...
user avatar
39 votes
Accepted

How to delete an element from a mapping?

Yes, delete user[someAddress]; will work with structs that do not contain a mapping. For this question, because name and nickname are not mappings, they will be deleted (set to zero) automatically: ...
user avatar
  • 78.8k
38 votes
Accepted

What is the zero, empty or null value of a struct?

You check that a value is defined in the mapping by checking it is not zero. If an explicit setting of zero has meaning for your application, you need auxiliary data (or structure) to track when a ...
user avatar
  • 78.8k
33 votes
Accepted

Can I save structs in a Mapping?

You can store structs as the values of your mapping, not as the key. Here is some more info: http://solidity.readthedocs.org/en/latest/types.html#structs struct Funder { address addr; uint amount;...
user avatar
  • 1,025
31 votes
Accepted

Initialising structs to storage variables

The former doesn't work because a SomeStruct storage is a pointer to the contract's storage space, and the way Solidity handles storage requires deriving that address from the organization of the ...
user avatar
  • 7,649
28 votes
Accepted

What is the zero value for a string?

One way is to check for the length of a String: if (bytes(m1[msg.sender]).length != 0 && bytes(m2[msg.sender].someString).length != 0) // do your thing See the answer I posted here
user avatar
  • 6,043
27 votes
Accepted

Child contract vs struct

Under most circumstances, data structures, even complicated ones, should be structs. Here are some reasons to choose structs: Contracts are more expensive. You'll have to pay for the contract's ...
user avatar
22 votes
Accepted

How to initialize an empty array inside a struct?

There is no need to initialize storage arrays in Solidity. Only memory arrays has to be initialized before usage. So in your case, no need to initialize x inside Bar as long as you are not assigning ...
user avatar
21 votes

Stack too deep, try removing local variables

Update Sep 18 2018 I just re-tested and the following works: pragma solidity ^0.4.25; contract Test { struct User{ uint256 uuid; bytes32 first_name; bytes32 last_name; ...
user avatar
20 votes
Accepted

Initialize struct with an empty array

The following will work: contract Test { struct Object { uint a; string b; string[] c; mapping(uint => uint) d; } Object field; function Test() { ...
user avatar
18 votes
Accepted

Nested structs in solidity?

You can store a struct in a struct. pragma solidity ^0.4.11; contract Nest { struct IpfsHash { bytes32 hash; uint hashSize; } struct Member { IpfsHash ipfsHash; } mapping(...
user avatar
  • 51.9k
14 votes

Solidity Error: Struct containing a (nested) mapping cannot be constructed

from 0.7.0 do like below: struct Request{ string description; uint value; address recipient; bool complete; uint approvalsCount; ...
user avatar
13 votes
Accepted

How to initialize empty struct array in new struct?

Some issues: The compiler does not yet support copying memory struct arrays to storage, so things[id] = thing; will fail. Thing thing; will initialize thing to a Thing with thing.items set to an ...
user avatar
  • 34.5k
12 votes
Accepted

How to create an iterable key->value structure in Solidity?

You could just store the index of the mapping in an array. uint[] indexes; mapping (uint => uint) example; function add(uint x){ example[indexes.length] = x; indexes.push(indexes.length); } ...
user avatar
  • 1,896
11 votes
Accepted

Solidity Error: Struct containing a (nested) mapping cannot be constructed

The problem is in the construction, i.e., Request({ description: description }) Therefore changing the location of newRequest from memory to storage will not help. If you really want to make it work, ...
user avatar
  • 316
10 votes
Accepted

Is it possible to have mappings inside mappings?

I suppose customStruct is a struct with timestamp property. Then your code should work. However here my test scenario: import "dapple/test.sol"; contract MyTest is Test { struct Struct { uint ...
user avatar
  • 881
10 votes

Efficient approach to delete element from array in Solidity

Source: https://github.com/su-squares/ethereum-contract/blob/master/contracts/SuNFT.sol Here you go: Algorithm: uint[] assets; mapping(uint=>uint) indexOfAsset; function removeAssetFromArray(uint ...
user avatar
10 votes
Accepted

Passing Struct as an argument in call

By using abi.encodeWithSignature, your struct successfully to the low-level call. The code below is modified based on what you provided. pragma solidity ^0.4.24; pragma experimental ABIEncoderV2; ...
user avatar
9 votes

Can I save structs in a Mapping?

Yes, you can. For example, in the solidity code below. There is a struct User that has a mapping of friends, from address to another struct Friend. Then there's a mapping of users, from address to ...
user avatar
9 votes
Accepted

How to test if a struct state variable is set

Question is possibly a duplicate of this: What is the zero, empty or null value of a struct? There are a few ways. You can check a value if you're sure it implicitly indicates a set/not set ...
user avatar
  • 51.9k
9 votes

Solidity function to return a data struct

You currently cannot return structs in Soldity as the answer to the question that you linked correctly outlines. But you can take your struct apart and return the individual elements (Solidity does ...
user avatar
  • 8,114
9 votes
Accepted

Is reading data free unlike a transaction?

As long as you are only reading data from the blockchain and not changing the blockchain, you don't need to carry out a transaction. The function you call is then carried out only by the node you are ...
user avatar
  • 1,751
9 votes

How Do I Update A Struct Property in Solidity

This line: User memory user = users[msg.sender]; Should use storage instead: User storage user = users[msg.sender]; If you use memory, then you're making a copy of the struct in memory and then ...
user avatar
  • 26.7k
9 votes

using mappings inside structs

The problem is that mappings can only live in storage. When you define PoolStruct memory pool;, the mapping member cannot be created in memory, and therefore the memory struct should be treated as if ...
user avatar
  • 316
8 votes

What is the zero, empty or null value of a struct?

There's really no such thing as "empty". An uninitialized index of the mapping is simply equal to the " zero" value of the proper type. To check if a value has been assigned, just check if balances[...
user avatar
  • 34.5k
8 votes
Accepted

Solidity: Initialize struct containing string array

The other answer which didnt work brought me on track to find it out myself: contract testStruct { struct stru{ string[] s; } stru myStru; function add(string s) { ...
user avatar
  • 8,114
8 votes

Returning a struct and reading via Web3

Starting from solidity 0.8.0 you can return a struct directly. Here is a simple example contract: pragma solidity ^0.8.0; contract Example { struct Store { string id; ...
user avatar
  • 223
8 votes
Accepted

Access struct object of one contract from another

In the constructor for Student, the mapping studentNames and the uint ID are not initialised. If you try to do stu s = studentNames[ID], you will just get 0. You want something like the following: ...
user avatar
  • 826

Only top scored, non community-wiki answers of a minimum length are eligible