htod - convert C .h files to D import files

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat May 27 11:44:27 PDT 2006



Rémy Mouëza wrote:
>>>But if that happened, things lke concatenation would cause problems.
>>>
>>>#define FOO "foo"
>>>#define BAR "bar"
>>>  |
>>>  |
>>>  V
>>>const char[] FOO = "foo\0"; //explicitly null terminate
>>>const char[] BAR = "bar\0"; //explicitly null terminate
>>>
>>>. . .
>>>
>>> char[] x = FOO ~ BAR; // Now it has an embedded \0 !
> 
> 
> I thought that D managed the "\0" characters ( that was why we hadn't to put any
> in our char [] variables ). Therefore I made the following test :
> // constchar.d
> import std.stdio ;
> 
> const char [] foo  = "foo\0" ;
> typeof ( foo ) bar = "bar\0" ;
> 
> void main ()
> {
> char [] test = foo ~ bar ;
> 
> writefln ( test );
> }
> ray at Moonraker:~/dee/tmp$ ./constchar
> foobar
> 
> Seems that there is no problem. I may have not understand something.
> 
> 

With DMD 0.158 on Windows, I got "foo bar" from this.  Note the space between.  Also note 
that writef is written in D, for D, and therefore doesn't use null characters as 
end-of-string (it relies on the .length property instead).  The big problem had to do with 
sending these strings to C code, which /will/ see the null's as an end-of-array marker. 
Try replacing your writefln with a printf, and you will only get "foo" printed to the screen.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-announce mailing list