To Walter, about char[] initialization by FF

Oskar Linde oskar.lindeREM at OVEgmail.com
Wed Aug 2 00:11:48 PDT 2006


Andrew Fedoniouk wrote:
>> But maybe that's because I never leave things at their defaults.  It's
>> like writing a story where you expect the reader to think everyone has 
>> brown eyes unless you say otherwise.
>>
> 
> Consider this:
> 
> char[6] buf;
> strncpy(buf, "1234567", 5);
> 
> What will be a content of you buffer?
> 
> Answer is: 12345\xff . Surprise? It is.

Not really surprising. Had you compiled this in a C program (you are 
using C functions after all), you would have gotten:

12345\x?? <- some garbage. Not a zero terminated string.

My manual for strncpy explicitly states:

" if there is no null byte among the first n
        bytes of src, the result will not be null-terminated."

/Oskar



More information about the Digitalmars-d mailing list