default initialization of char arrays

jmh530 john.michael.hall at gmail.com
Wed Oct 15 16:43:12 UTC 2025


On Monday, 8 September 2025 at 15:42:27 UTC, Walter Bright wrote:
> 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;
> ```

At least the compiler is smart enough to know that the following 
is an error.

```
void main()
{
	char[10] xxx = [0];
}
```


More information about the Digitalmars-d mailing list