Need strnlen().

Ephraim Vishniac vishniac at wanginst.UUCP
Thu Nov 14 23:25:18 AEST 1985


> I think that there should be a function called "strnlen" as follows:
> 
> int strnlen (string, size)
> 	char	*string;
> 	int	size;
> 
> where "size" is the maximum number of bytes in "string".
> 
I agree that such a facility is needed, but I don't think it will ever
be provided as "standard" C.  The basic problem (or the C problem, if you
prefer :-) is that C defines a representation for strings, but leaves
the user to implement the operations.  This allows one to cook up all sorts
of invalid strings (such as the unterminated ones the original poster is
worried about).

To my mind, a string consists of three things:
	1.  The characters of the string;
	2.  The length of the string, either as such or encoded by marking
		the string;
	3.  The storage block where the string is located, which has its
		own attributes (alignment and size, to name two).
C has no problem with the first (the characters are easy to access); some
problems with the second (null termination is good for some purposes,
rotten for others); and completely ignores the third.

But: since this is C, you don't have to use the standard representation and
functions.  Just as I did when sufficiently burned, you can use your own
representation and macros.  Then the only problem is that nobody will use
your modules, because they're "non-standard".

-- 
Ephraim Vishniac
  [apollo, bbncca, cadmus, decvax, harvard, linus, masscomp]!wanginst!vishniac
  vishniac%Wang-Inst at Csnet-Relay



More information about the Comp.lang.c mailing list