default initialization of char arrays

Walter Bright newshound2 at digitalmars.com
Mon Sep 8 15:42:27 UTC 2025


This came up in an email exchange. Consider:

```
import core.stdc.stdio;

__gshared char[10] xxx = [0]; // initialize xxx to all zeros

void main()
{
     foreach (c; xxx)
         printf("%d\n", c);
}
```

A `char` default initializes to 0xFF. The programmer wanted to default 
initialize the array of char to 0, and so used [0] to initialize it. This 
resulted in `[0,255,255,255,255,255,255,255,255,255]`. He asked how to default 
initialize it to 0 without having to tediously enumerate the 0 for each element 
in the initializer.

The answer is:
```
__gshared char[10] xxx = 0;
```


More information about the Digitalmars-d mailing list