Philip Potter wrote:
> Ralph D. Ungermann wrote:
>> Hal Vaughan wrote:
>>> Since a NULL terminates a string
>>
>> Nope. But there is the concept of "Null-Terminated Byte Strings"
(NTBS):
>> many functions treat a '\0' as the end of the string.
>
> In C, a string is terminated by a '\0' by definition (see n1256, the
> latest C draft, section 7.1.1). If it isn't terminated by a '\0', it's
> not a string.
Thanks! I've been looking closely to "6.4.5 String Literals", but
somehow I missed the footnote, and hence the full definition of "string".
For total confusion: C++ talks about "Character sequences" and
"null-terminated byte strings" (17.3.2.1.3). Both seem to allow '\0'
anywhere, while a C string definitely ends with the first '\0'.
But the message to the OP is: A '\0' in the middle is absolutely ok in
C/C++, but you must not name it "string". His terminus "series of codes"
is fine, he just must take care not to mix it with "strings".
> Nevertheless, a string literal may not be a good choice if it is to be
> output as an array of bytes, because of the implicit '\0' at the end:
I'm still familar with C89 constructs like
char key[4] = "1234"; // no '\0' appended
but now I find, that this is ill-formed in C++. Hence, I fully agree,
that string literals are not merely doubtful, but totally useless for
the OP's purpose.
>> unsigned char dev_out[6] = { '\0x03', '\0', 0, 255, 'x', '\r' };
>
> ITYM '\x03' above.
Yes, of course!
-- ralph


|