"need `this` for `s` of type `char*`" error message

Mathias LANG geod24 at gmail.com
Thu Nov 11 07:15:04 UTC 2021


On Thursday, 11 November 2021 at 07:10:39 UTC, Mathias LANG wrote:
> [...]
>
Your type definition is wrong:
```D
struct toml_datum_t {
   int ok;
   union u {
     toml_timestamp_t* ts; /* ts must be freed after use */
     char*   s; /* string value. s must be freed after use */
     int     b; /* bool value */
     int64_t i; /* int value */
     double  d; /* double value */
   }
}
```

If you check the size of this struct, it's going to be 4,
because `u` is a type definition. What you want is either:

```D
struct toml_datum_t {
   int ok;
   union {
     toml_timestamp_t* ts; /* ts must be freed after use */
     char*   s; /* string value. s must be freed after use */
     int     b; /* bool value */
     int64_t i; /* int value */
     double  d; /* double value */
   }
}
```

Which you access via `host.s` or:

```D
struct toml_datum_t {
   int ok;
   /// This is the type definition
   union U {
     toml_timestamp_t* ts; /* ts must be freed after use */
     char*   s; /* string value. s must be freed after use */
     int     b; /* bool value */
     int64_t i; /* int value */
     double  d; /* double value */
   }
   /// This is the field
   U u;
}
```

Note that instead of doing this work yourself, I would highly 
recommend the excellent 
[dstep](https://github.com/jacob-carlborg/dstep).

Fixed formatting (so much for "Fix it for me").


More information about the Digitalmars-d-learn mailing list