4

I would like to initialize an array of structs:

struct person { string firstName; string lastName; } person[3] family = [ person("Will", "Smith"), person("Jada", "Smith"), person("Brad", "Pitt") ] 

I get a compile error:

"UnimplementedFeatureError: Copying of type struct [...] memory to storage not yet supported.

I just want to initialize an array of Structs. I'd rather not push new instances of "person" one by one in a constructor. Any ideas on what I should do?

3 Answers 3

0

Initialising a storage array of structs right after declaring it is not implemented yet.

You can, however, declare it and then assign values to it in a function (be it the constuctor or another one)

struct Person { string firstName; string lastName; } Person[3] public family; constructor() { family[0] = Person("Will", "Smith"); family[1] = Person("Jada", "Smith"); family[2] = Person("Brad", "Pitt"); } 
0

Quite Simple.

struct Person { string firstName; string lastName; } Person[] public persons; Person[4] public people; // `people` array will be an array of the Person struct that contains only 4 elements of type `Person` struct. persons.push(Person(firstName value, lastName value)); // To push to the people array 👇🏾 people[index] = Person(firstName value, lastName value); // Where index can be within 0 and (4 - 1) 

The array to work with will then be persons or the people depending on your choice.

0

In Solidity, initializing an array of structs with predefined values directly in the declaration is not straightforward due to certain limitations in how memory and storage data are handled. As of my last update, Solidity doesn't support the direct initialization of an array of structs with predefined values in the way you've shown in your example. The error you're encountering is due to this limitation.

However, you can achieve your goal by initializing the array within a constructor. While you prefer not to use the push method in a constructor, it's currently one of the viable ways to initialize an array of structs with predefined values.

Here's how you can modify your code to achieve this:

pragma solidity ^0.8.0; contract MyContract { struct Person { string firstName; string lastName; } Person[3] public family; constructor() { family[0] = Person("Will", "Smith"); family[1] = Person("Jada", "Smith"); family[2] = Person("Brad", "Pitt"); } } 

In this approach:

The family array is declared without initial values. The constructor of the contract is used to initialize each element of the array. This is done by directly assigning a new Person struct to each index of the family array. This method ensures that your family array is initialized with the desired values when the contract is deployed, without needing to call push multiple times. It's a straightforward workaround for the current limitation in Solidity regarding direct array of struct initialization.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.