0Pricing
Web3 & DApp Development Fundamentals · 강의

참조 타입

배열, 구조체, 매핑

참조 타입은(는) CoddyKit의 무료 Web3 & DApp Development Fundamentals 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Web3 & DApp Development Fundamentals 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Web3 & DApp Development Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Are Reference Types?

Reference types describe data that is too large to fit in one slot, so variables refer to a location rather than holding the data directly.

The three reference types are arrays, structs, and mappings. When you use them you must think about their data location: storage, memory, or calldata.

Fixed-Size Arrays

A fixed-size array has a length set at compile time and cannot grow. The type uint[3] always holds exactly three values.

Use .length to read the size. Out-of-bounds access reverts.

<code>uint[3] public scores = [10, 20, 30];

function first() public view returns (uint) {
    return scores[0];
}</code>

Dynamic Arrays

A dynamic array can grow and shrink. Declare it as uint[]. In storage you can push to append and pop to remove the last element.

<code>uint[] public nums;

function add(uint n) public {
    nums.push(n);
}

function removeLast() public {
    nums.pop();
}</code>

Bytes and String

bytes is a dynamically-sized byte array and string is its UTF-8 text cousin. Both are reference types.

Note: string has no .length or indexing; convert to bytes first to inspect raw bytes.

<code>string public name = 'Alice';

function nameLength() public view returns (uint) {
    return bytes(name).length;
}</code>

Defining Structs

A struct groups related variables into one custom type. It is ideal for modeling entities like users, orders, or tokens.

<code>struct User {
    string name;
    uint256 balance;
    bool active;
}</code>

Using Structs

You can create a struct with positional or named arguments and store it. Reading a struct from storage gives you a reference you can modify.

<code>User public owner;

function setup(string memory n) public {
    owner = User({ name: n, balance: 0, active: true });
}</code>

Mappings

A mapping is a key-value store, like a hash table. It is one of the most used structures in smart contracts, for example tracking balances per address.

Keys are not stored, so you cannot iterate a mapping or get its length.

<code>mapping(address => uint256) public balances;

function deposit() public payable {
    balances[msg.sender] += msg.value;
}</code>

Nested Mappings

Mappings can be nested to model relationships, such as allowances where one owner permits multiple spenders.

This pattern is the heart of the ERC-20 allowance system.

<code>mapping(address => mapping(address => uint256)) public allowance;

function approve(address spender, uint256 amount) public {
    allowance[msg.sender][spender] = amount;
}</code>

Combining Structs and Mappings

A powerful pattern is mapping keys to structs. This lets you store rich records keyed by an id or address.

<code>struct Order {
    uint256 amount;
    bool paid;
}

mapping(uint256 => Order) public orders;

function createOrder(uint256 id, uint256 amt) public {
    orders[id] = Order({ amount: amt, paid: false });
}</code>

Arrays of Structs

You can keep a dynamic array of structs when you need ordered, iterable records. Combine it with a mapping for fast lookup.

<code>struct Item { string name; uint256 price; }
Item[] public items;

function addItem(string memory n, uint256 p) public {
    items.push(Item(n, p));
}</code>

Reference Semantics

When you assign a storage reference type to a local storage variable, both point to the same data. Changes through one are visible through the other.

A memory copy, in contrast, is independent.

<code>uint[] public data;

function tweak() public {
    uint[] storage ref = data; // alias
    ref.push(42);              // also changes data
}</code>

Quick Check

Test your understanding of reference types.

Recap

You explored Solidity reference types:

  • Arrays (fixed and dynamic), plus bytes and string
  • Structs to group related fields
  • Mappings and nested mappings for key-value storage
  • Powerful combinations like mapping-to-struct and arrays-of-structs

Reference types share locations, so always be deliberate about storage versus memory.

자주 묻는 질문

“참조 타입” 강의는 무료인가요?

네 — “참조 타입” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Web3 & DApp Development Fundamentals 강의 전체를 잠금 해제할 수 있습니다. Web3 & DApp Development Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

“참조 타입”에서 뭘 배우나요?

배열, 구조체, 매핑 브라우저에서 직접 실행하는 실습 코드로 Web3 & DApp Development Fundamentals을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Web3 & DApp Development Fundamentals을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Web3 & DApp Development Fundamentals은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“참조 타입” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Web3 & DApp Development Fundamentals 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Web3 & DApp Development Fundamentals 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 값 타입
  2. 참조 타입
  3. 스토리지와 메모리 비교
  4. 상수와 불변값
← Web3 & DApp Development Fundamentals(으)로 돌아가기