Another silly question

Guy Harris guy at auspex.auspex.com
Sat May 27 05:02:30 AEST 1989


 >>I think the "second-classedness" of arrays helps give C its elegant
 >>syntax.  Any other examples of the "problems" it causes?
 >
 >I think of C arrays as syntactic sugar for initialized pointers.

In other words, your answer to his question is that one problem caused
by the "second-classedness" of arrays is that it leads people to think
of them, incorrectly, as pointers?  I'd certainly agree with that....

 >Thus
 >
 >	char foo[] = "I am an anonymous char *";
 >
 >is an abbreviation for
 >
 >	register char *const foo = "I am an anonymous char *";
 >
 >I reason 'const' because the value of the pointer cannot be changed,
 >and 'register' because the address of the pointer cannot be taken.

Well, unfortunately, there's no little thing you can add to the
declaration to straightforwardly reflect the fact that:

	foo.c:

		...

		char foo[] = "I am an array";

		...

	bar.c:

		...

		extern char *const foo;

		...

is wrong.  (And yes, "I did (the above); why isn't it working?" has
appeared as a question in this newsgroup in the past, so people really
*do* get the idea that it's supposed to work.)  If you really want to go
out of your way, I guess the "register" does that - but it also hints
that something gets stuffed into a register, which is wrong. 

Think of arrays as arrays, pointers as pointers, and array-valued
expressions being converted, in most but *not* all contexts, as being
converted to pointer-valued expressions that point to the first element
of the array, and you won't go wrong.  That may be more *complicated*
than your model, but it has the advantage of reflecting reality....



More information about the Comp.lang.c mailing list