Created
July 12, 2023 01:05
-
-
Save TehilaFavourite/5afaad0be7f83277aa7b555b3eb0e98a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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