February 08, 2004 [bug] String initializers | ||||
---|---|---|---|---|
| ||||
The following program prints garbage in DMD 0.79 on Linux: import std.c.stdio; int main() { char[4] str = "abc"; printf("%.*s\n", str); return 0; } It works if str is declared as having three elements, or if str is declared static. It also gives garbage (and no error) when str is declared as having fewer than three elements. If an array initializer is used instead, DMD complains that str needs to be declared "static", and it also complains if there are too many initializers (but not too few, though the spec says it should). This one segfaults: int main() { static char[] str = "abc"; str[0] = 'A'; return 0; } If an array initializer is used instead, it works. Are string initializers, and static initializers for dynamic arrays, allowed at all? I couldn't find any mention of array initializers in the spec other than "Static Initialization of Static Arrays", and it's not clear whether the second static means static-as-in-fixed- length or static-as-in-uses-the-static-keyword. BTW, the example shown for enum-array initialization in that section of the spec won't work; the size of the array will be one too small, and the enum names need to be qualified. Is there any way to bring the enum names into the current namespace (other than by manually aliasing them all)? Neither "import" nor "with" want anything to do with an enum. -Scott |
Copyright © 1999-2021 by the D Language Foundation