strings

Chris Torek chris at mimsy.UUCP
Sun May 14 11:36:42 AEST 1989


In article <456 at sdti.SDTI.COM> turner at sdti.SDTI.COM (Prescott K. Turner)
writes:
>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.

It is true that strings---or rather, string constants; C does not have
strings as a basic data type: they are merely a convention, which some
programs (Emacs, e.g.) avoid---are second class objects.  This is because
a double-quoted string constant creates an unnamed array, and C's arrays
are second-class.  But this only prevents cleverness in a weak sense.
If you prefer counted strings, you can create them:

	struct cstr {
		int	len;
		char	*data;
	};
	#define CSTR(s) { sizeof(s) - 1, s }

	struct cstr hello = CSTR("hello world");

It is true that the compiler and run-time system cannot arbitrarily
choose some alternative representation for C's strings, but neither
can they choose other representations for any other form of array.
The language is at least consistent.

Incidentally, the average string (in the mythical average C program)
is shorter than the average Dhrystone string.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list