mixing pointers and arrays (arrays by value)

arnold at gatech.UUCP arnold at gatech.UUCP
Thu Aug 18 00:20:06 AEST 1983


	I think the way to change C to copy arrays by value is to add a
new operator to the language. I suggest using the ` (back quote). This is
one of 3 characters which are not used in C at all (others are $ and @).
It would work something like this:

char x[10],y[10];	/* declaration of two arrays */

`x = `y;	/* copy array y to array x, equivalent to strcpy(x,y) */

foo(`y);     /* call procedure foo() with the entire array y sent by value */

	A procedure receiving an array passed by value would declare it
as follows:

foo(array)
char `array[];	/*  the [] says 'array', the ` says it is passed by value */
{
	/* code */
}


	So far, this would allow array assignment, without changing the meaning
of 'array name as pointer to first element'. It is also upward compatible,
and would not break any existing programs. The use of an explicit operator makes
it 100% clear that array operations are going on.
	The only problem with this is assignment of character strings; one
would have to do something ugly like:

	`x = ` "string constant";

however, the language could probably special-case it to make the ` before
the constant un-necessary, the same way that it special-cases initialization
of character arrays to allow using "string" instead of the more verbose
{ 's', 't', 'r', 'i', 'n', 'g', '\0' }. As with initialization, where the
braces form is allowed, the ` in front of a string constant would also be
valid (i.e. no error messages), just not necessary.

	An array concatenation operator, probably $, might also be useful.
For example:

#define DIM	/* whatever */
char x[DIM], y[DIM], z[DIM];

x = y $ z;		/* same as sprintf(x, "%s%s", y, z) */

x $= y;		/* the more common strcat(x,y); */

	Both the ` and the $ should work for arrays of any type, not
just character arrays, but character arrays would be where they were the
most useful. They should not work for arrays of mixed type.
	New operators would make it easy for lint to catch mistakes as
well, for instance  strcat(x, `y)  would be a type error, an array passed
instead of a pointer.
	C could use some extensions for dealing with arrays, we just have
to be *very* careful about how well they fit in with the rest of the language.
-- 
Arnold Robbins
Arnold @ GATech  (CS Net)	Arnold.GaTech @ UDel-Relay  (ARPA)
...!{sb1, allegra}!gatech!arnold  (uucp)
...!decvax!cornell!allegra!gatech!arnold



More information about the Comp.lang.c mailing list