strings

Leslie Mikesell les at chinet.chi.il.us
Wed May 17 03:58:57 AEST 1989


In article <10250 at socslgw.csl.sony.JUNET> diamond at csl.sony.co.jp.csl.sony.co.jp (Norman Diamond) writes:

>>If you want counted strings, C makes it relatively easy to provide
>>them for yourself.

>Yes.  You throw away the C library (which I understand is part of the
>proposed ANSI standard) and the language's definition of how strings
>are represented, you define your own representation of strings, and
>you implement your own library.  This is perfectly fine.  A strictly
>conforming program is not required to use every feature or every
>mis-feature of the standard; a program is allowed to be more strict.

Why throw anything away?  You can store the lengths elsewhere and
still use the null-terminated representation that the library
routines want to see.
 struct eg { unsigned int str_len;
             char *the_str;
            };
This way you only need to store the length when you think it will be
useful later.  The only problem occurs if you need to store a '\0'
value as part of the character array, and then it is only a problem if
you want to use the library string routines on that array.

>Good luck porting other people's strictly conforming programs though.
>They might use C strings.
 
As well they should...  
The only real problem I see with the C library routines is that they
generally don't return a length or pointer to the last character even
though it would be trivial to do so.  Thus if you want the length you
have to make another function call (at least strlen doesn't return
the same pointer you gave it).

Les Mikesell



More information about the Comp.lang.c mailing list