In the C standard library, strings are implemented using an array of chars, terminated by a null character: '\0'. Such ASCIZ strings lead to inefficiency because every time we need to know the length of a string, we need to iterate over it looking for '\0'.
The way around this is to store the length of the string when we create it, e.g. using a struct as follows:
typedef struct cstring_ {
size_t nchars;
char chars[0];
} cstring;
Has anyone made a shared library implementing the string.h functions, but using a struct instead of char *
to pass strings around?
If not, is there a specific reason why this would be a bad idea?
There are probably dozens of those. Have a look at Glib's GString for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With