Structs help you group related data together in one place. They make your code easier to understand and manage.
Structs in Blockchain / Solidity
struct StructName {
Type1 variable1;
Type2 variable2;
// more variables
};struct Person {
string name;
uint age;
};struct Product {
string productName;
uint price;
uint quantity;
};This simple contract defines a Product struct with a name and price. It sets a product in the constructor and lets you get its details.
pragma solidity ^0.8.0; contract Store { struct Product { string name; uint price; } Product public product; constructor() { product = Product("Book", 10); } function getProduct() public view returns (string memory, uint) { return (product.name, product.price); } }
Structs help keep related data organized and easy to access.
You can create variables of struct type to store data.
Structs improve code readability and reduce errors.
Structs group related data into one unit.
Use structs to organize complex data clearly.
Structs make your blockchain code easier to manage.
Practice
What is the main purpose of using structs in blockchain programming?
Solution
Step 1: Understand what structs do
Structs are used to group different pieces of related data into one unit.Step 2: Compare options
Only To group related data together under one name correctly describes grouping related data. Other options describe unrelated tasks.Final Answer:
To group related data together under one name -> Option BQuick Check:
Structs = Group related data [OK]
- Confusing structs with loops or calculations
- Thinking structs store only single values
- Assuming structs speed up math operations
Which of the following is the correct syntax to define a struct named Person with fields name (string) and age (uint)?
?
Solution
Step 1: Recall struct syntax in blockchain languages
Structs are defined with the keywordstruct, followed by the name and curly braces containing fields with types and names.Step 2: Check each option
struct Person { string name; uint age; } matches the correct syntax. Options B, C, and D use incorrect punctuation or order.Final Answer:
struct Person { string name; uint age; } -> Option DQuick Check:
Correct struct syntax = struct Person { string name; uint age; } [OK]
- Using parentheses instead of braces
- Assigning struct like a variable
- Omitting semicolons after fields
Given the struct and code below, what will be the output?
struct Car {
string brand;
uint year;
}
Car myCar = Car("Tesla", 2020);
print(myCar.brand);Solution
Step 1: Understand struct initialization
The structCaris created with brand "Tesla" and year 2020.Step 2: Identify printed field
The code printsmyCar.brand, which is "Tesla".Final Answer:
Tesla -> Option AQuick Check:
Print myCar.brand = Tesla [OK]
- Printing the whole struct instead of a field
- Confusing brand with year
- Expecting an error when printing a field
What is wrong with the following struct usage?
struct Book {
string title;
uint pages;
}
Book myBook;
myBook.title = "Learn Blockchain";
myBook.pages = "300";Solution
Step 1: Check field types and assignments
The fieldpagesis of typeuint, but assigned a string "300".Step 2: Identify type mismatch error
Assigning a string to a number type causes a type error.Final Answer:
Cannot assign string to pages which is uint -> Option CQuick Check:
Type mismatch in assignment = Cannot assign string to pages which is uint [OK]
- Assigning string to a number field
- Ignoring type errors
- Thinking struct names must be lowercase
You want to store multiple Employee structs in a list and then find the average age. Which approach correctly uses structs and arrays?
struct Employee {
string name;
uint age;
}
Employee[] employees;
employees.push(Employee("Alice", 30));
employees.push(Employee("Bob", 40));
uint totalAge = 0;
for (uint i = 0; i < employees.length; i++) {
totalAge += employees[i].age;
}
uint averageAge = totalAge / employees.length;
print(averageAge);Solution
Step 1: Understand struct array usage
The code creates an array ofEmployeestructs and adds two employees.Step 2: Check loop and calculation
The loop sums ages correctly usingemployees[i].ageand divides by length to get average.Final Answer:
This code correctly calculates and prints average age -> Option AQuick Check:
Struct arrays and loops = correct [OK]
- Using <= instead of < in loop causing out-of-bounds
- Thinking structs can't be stored in arrays
- Trying to access struct fields without indexing
Ready to prove it?
Pass the test and this pattern moves unknown
