In the C standard library, strings are implemented using an array of characters ending with a null character: '\ 0'. Such ASCIZ strings lead to inefficiency, because every time we need to know the length of the string, we need to iterate over it, looking for "\ 0".
The way around this is to preserve the length of the string when creating it, for example. using the following structure:
typedef struct cstring_ {
size_t nchars;
char chars[0];
} cstring;
Has anyone created a shared library that implements string.h functions, but using struct instead char *to pass strings around?
If not, is there a specific reason why this would be a bad idea?
source
share