How would the equivalent C type be in D?

FeepingCreature feepingcreature at gmail.com
Wed Mar 1 08:32:25 UTC 2023


On Wednesday, 1 March 2023 at 08:26:07 UTC, FeepingCreature wrote:
> ```d
> uint32_t[1] value;
> value[0] = screen.black_pixel;
> xcb_create_gc(connection, black, win, mask, value.ptr);
> ```

To expand on this:

```d
uint32_t[2] value;
uint32_t* value_ptr = value.ptr;
// We are allowed to access the pointer at index 0
// because we declared the value it points to, to be size 2.
value_ptr[0] = 0;
// This is also allowed, because `value_ptr` is
// a pointer to two sequential `uint32_t` in memory.
value_ptr[1] = 0;
// But this access would segfault, because it's trying to write
// to the third element of a two-element array:
value_ptr[2] = 0;
```

Note: I just lied; `value_ptr[2]` would not segfault, for 
somewhat technical reasons; but it *would* corrupt your program's 
memory. At any rate, it's an invalid operation.



More information about the Digitalmars-d-learn mailing list