Printing struct fields and values

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Thu Jan 22 16:35:11 UTC 2026


```d
struct S {
     int field0;
     int* field1, field2;
}

void main() {
     import std.stdio;
     S input;
     input.field1 = new int(2);

     foreach(v; input.tupleof) {
         write(__traits(identifier, v), " = ");

         static if (is(typeof(v) : T*, T)) {
             if (v is null)
                 writeln("null");
             else
                 writeln(*v);
         } else {
             writeln(v);
         }
     }
}
```


More information about the Digitalmars-d-learn mailing list