Skip to content

Instantly share code, notes, and snippets.

@TehilaFavourite
Created July 12, 2023 01:05
Show Gist options
  • Save TehilaFavourite/5afaad0be7f83277aa7b555b3eb0e98a to your computer and use it in GitHub Desktop.
Save TehilaFavourite/5afaad0be7f83277aa7b555b3eb0e98a to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract StructInitializationExample {
struct Person {
string name;
uint age;
}
function initializeStruct() public pure returns (Person memory) {
// Initializing a struct using method 1: Direct assignment
Person memory person1 = Person("Alice", 25);
// Initializing a struct using method 2: Named arguments
Person memory person2 = Person({name: "Bob", age: 30});
// Initializing a struct using method 3: Individual assignments
Person memory person3;
person3.name = "Charlie";
person3.age = 35;
return person1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment