strings

Blair P. Houghton bph at buengc.BU.EDU
Sun May 14 21:46:05 AEST 1989


In article <456 at sdti.SDTI.COM> turner at sdti.UUCP (0006-Prescott K. Turner, Jr.) writes:
>In article <1989May11.155935.22324 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:
>>In article <10235 at socslgw.csl.sony.JUNET> diamond at csl.sony.junet (Norman Diamond) writes:
[...it's all in the ref's.  I'm after this guy:...]
>
>Diamond is right.  C is worse because it specifies not just the operations on
>string types and their meaning, but the representation of strings.  As Paul
>Abrahams put it in SIGPLAN Notices 23:10, "Some Sad Remarks About String
>Handling in C", "C strings are not first class objects."  He gives details of
>how this prevents the clever from succeeding.

Those ACM SIG* types are usually pretty clever, but if one can't come up
with the obvious (see below), then this one wonders where that one learned
what data were, and whether the full meaning of 'clever' doesn't apply...

	typdef struct {
		char *characters;
		int length;
	} string;

	#define assign_string(s,a) s.characters=a;\
				   s.length=strlen(s.characters);

	main()
	{
		string lunchbox;

		assign_string(lunchbox,"Batman");

		/* Now all you need do is refer to lunchbox.length
		 *	when you need the length of the string
		 *	stored in lunchbox.characters...
		 */
		
		printf("%d\n",lunchbox.length);

		/*
		 *	If you think this is less efficient
		 *	computationally than the stuff your
		 *	'intelligent' languages do with string
		 *	data, then you're sadly mistaken...
		 *
		 * If using lunchbox.length doesn't appeal to you,
		 *	try:
		 */

		printf("%d\n",stringlength(lunchbox);

		/*
		 *	where you've done
		 *	
		 *	#define stringlength(x) x.length
		 *
		 *	somewhere above the call to stringlength.
		 */
	}

				--Blair
				  "Too damn easy.  I have _got_
				   to be missing some undercurrent
				   in this stream of cruft..."



More information about the Comp.lang.c mailing list