0
0
Blockchain / Solidityprogramming~10 mins

Structs in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Structs
Define Struct with fields
Create instance of Struct
Access or modify fields
Use struct instance in code
End
Structs group related data together. You define a struct, create instances, access or change fields, then use them in your program.
Execution Sample
Blockchain / Solidity
struct Person {
  string name;
  uint age;
}

Person alice = Person("Alice", 30);
Defines a Person struct with name and age, then creates an instance named alice.
Execution Table
StepActionEvaluationResult
1Define struct Person with fields name and ageNo runtime actionStruct type Person created
2Create instance alice with name='Alice', age=30Person("Alice", 30)alice.name = 'Alice', alice.age = 30
3Access alice.namealice.name'Alice'
4Access alice.agealice.age30
5Modify alice.age to 31alice.age = 31alice.age = 31
6Use alice in function or logicUse alice fieldsData from alice used
7End of executionNo further stepsExecution stops
💡 Reached end of code execution
Variable Tracker
VariableStartAfter Step 2After Step 5Final
alice.nameundefinedAliceAliceAlice
alice.ageundefined303131
Key Moments - 3 Insights
Why do we need to define a struct before creating instances?
The struct defines the shape and types of data fields. Without it, the program doesn't know what data each instance should hold (see execution_table step 1).
Can we change the fields of a struct instance after creation?
Yes, struct fields can be modified after creation as shown in execution_table step 5 where alice.age changes from 30 to 31.
How do we access data inside a struct?
We use dot notation like alice.name or alice.age to get or set values, shown in steps 3 and 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is alice.age after step 5?
Aundefined
B30
C31
DAlice
💡 Hint
Check variable_tracker row for alice.age after Step 5
At which step is the struct Person defined?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table step descriptions
If we did not define the struct first, what would happen when creating alice?
AInstance created with default fields
BError because struct type is unknown
CFields automatically inferred
Dalice.age would be zero
💡 Hint
Refer to key_moments about struct definition necessity
Concept Snapshot
Structs group related data fields.
Define struct with field types.
Create instances with values.
Access or modify fields with dot notation.
Use instances in your code.
Structs help organize complex data.
Full Transcript
Structs are like blueprints for data. First, you define a struct with named fields and their types. Then you create an instance by giving values to those fields. You can access or change these fields using dot notation, like alice.name or alice.age. This helps keep related data together and easy to manage. The execution table shows defining the struct, creating an instance alice with name 'Alice' and age 30, accessing fields, modifying age to 31, and using the instance in code. Variables track how alice's fields change over time. Key moments clarify why defining the struct first is necessary, how to access fields, and that fields can be changed after creation. The quiz tests understanding of these steps and concepts.