Hacker News new | ask | show | jobs
by sirwhinesalot 403 days ago
You shouldn't use any of those garbage functions. Just ignore \0 entirely, manage your lengths, and use memcpy.
3 comments

I am not writing in C, but always wondered, why pascal-like strings wrappers are not popular, i. e. when you have first 2 bytes represent the length of the string following by \0 terminated string for compatibility.
2 bytes is not enough, usually you'll see whole "size_t" worth of bytes for the length.

But you could do something utf-8 inspired I suppose where some bit pattern in the first byte of the length tells you how many bytes are actually used for the length.

Pascal originally required you to specify the length of the string before you did anything with it.

This is a totally good idea, but was considered to be too much of a pain to use at the time.

In C you have to do that too, like... malloc()?
You still need a 0-terminated string to pass to API of most libraries (including ones included with the OS - in this case, Win32).
Yeah, Drepper said the same thing.