A struct in Solidity is a custom data type that allows developers to group related variables together. Structs are useful for organizing complex data structures, making it easier to manage and manipulate related data as a single entity. Each variable in a struct is called a "member," and members can have different data types.

Key Features of Structs

  • Custom Data Types: Structs allow you to define your own data types, which can encapsulate multiple properties.
  • Member Variables: Each member of a struct can be of any data type, including other structs, arrays, and basic types.
  • Storage: Structs can be stored in state variables, memory, or storage, depending on their usage.
  • Access: Members of a struct can be accessed using dot notation.

Sample Code for Structs


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract StudentRegistry {
// Define a struct to represent a student
struct Student {
string name; // Student's name
uint256 age; // Student's age
string grade; // Student's grade
}

// Mapping from student ID to Student struct
mapping(uint256 => Student) public students;

// Function to add a new student
function addStudent(uint256 id, string memory name, uint256 age, string memory grade) public {
students[id] = Student(name, age, grade); // Create and store a new Student
}

// Function to get student details
function getStudent(uint256 id) public view returns (string memory, uint256, string memory) {
Student memory student = students[id]; // Retrieve the student struct
return (student.name, student.age, student.grade); // Return student details
}
}

Explanation of the Sample Code

In the example above:

  • The contract StudentRegistry defines a struct called Student that has three members: name, age, and grade.
  • A mapping called students is created to associate a unique student ID (of type uint256) with a Student struct.
  • The addStudent function allows users to add a new student by providing an ID, name, age, and grade. It creates a new Student and stores it in the mapping.
  • The getStudent function retrieves the details of a student by their ID and returns the name, age, and grade.

Structs with Arrays

Structs can also contain arrays, allowing for more complex data structures. Here’s an example:


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CourseRegistry {
// Define a struct to represent a course
struct Course {
string name; // Course name
uint256 credits; // Number of credits
string[] students; // List of students enrolled in the course
}

// Mapping from course ID to Course struct
mapping(uint256 => Course) public courses;

// Function to add a new course
function addCourse(uint256 id, string memory name, uint256 credits) public {
courses[id] = Course(name, credits, new string[](0)); // Create a new Course with an empty student list
}

// Function to enroll a student in a course
function enrollStudent(uint256 courseId, string memory studentName) public {
courses[courseId].students.push(studentName); // Add the student's name to the course's student list
}

// Function to get course details
function getCourse(uint256 id) public view returns (string memory, uint256, string[] memory) {
Course memory course = courses[id]; // Retrieve the course struct
return (course.name, course.credits, course.students); // Return course details
}
}

Conclusion

Structs in Solidity are powerful tools for creating complex data types that group related variables together. They enhance the organization of data within smart contracts, making it easier to manage and manipulate. By using structs, developers can create more structured and maintainable code, which is essential for building robust smart contracts.