Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.
Version 1 (modified by Andrej08, 14 years ago)
--

Struct

Part of TutorialFundamentals

Description

Structs allow you to bind together basic data types into a more complicated paradigm.

Example

struct Vehicle
{  
    double cost;
    int wheels;
}

struct Utensil
{ 
    bool pointy;
    bool tined;  
}

void main()
{      
    Vehicle car;
    car.cost = 20_000;
    car.wheels = 4;
    
    Vehicle motorcycle;
    motorcycle.cost = 2_000;
    motorcycle.wheels = 2;
    
    Vehicle boat;
    boat.cost = 5_000; 
    boat.wheels = 0;


    Utensil spoon;
    spoon.pointy = false;
    spoon.tined = false;
    
    Utensil fork;
    fork.pointy = true;
    fork.tined = true;
    
    Utensil knife;
    knife.pointy = true;
    knife.tined = false;
}

Source

Based on struct.html by jcc7.