Clarification needed on Pointers/Arrays

sabbagh sabbagh at acf3.NYU.EDU
Fri Feb 24 03:54:36 AEST 1989


In article <11840 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>In article <889 at acf3.NYU.EDU> sabbagh at acf3.UUCP () writes:
>>In article <1436 at etive.ed.ac.uk> sam at lfcs.ed.ac.uk (S. Manoharan) writes:
>
>>Now, according to K & R, the notation
>>	p1[j]
>>is in ALL WAYS equivalent to
>>	*(p1 + j)
>
>Minor nit: they are equivalent in expressions, but not in declarations.

Agreed.

>
>>(Incidentally, this implies that p1[j] == j[p1]) !!
>
>A fact used primarily by the Obfuscated C Contestants.  I wish X3J11 had fixed
>this.  (They could use the same justification that they did for fixing `+ ='.)
>
>>So what are arrays?  They are POINTER CONSTANTS.
>
>Urk.  While this is true in a sense, it's been my observation that people who
>think of them that way are taking the wrong path to understanding.

Hmm. It depends on what you are trying to understand.  If you are trying to 
USE C, then it's the perfect way to understand them.

>It becomes
>a pointer after the transformation (array-valued expressions decay into
>pointer-valued expressions when used in an rvalue context), in which case its
>value is constant throughout its lifetime.  This is independent of its lvalue
>properties, but some people believe that this is the reason that the lvalue is
>not modifiable.  This is not the case; it would be a relatively simple
>extension to the language to allow array copy.  (It is hindered only by the
>fact that X3J11 didn't fix the declaration botch noted in the first warning
>message above (and in fact paved the way for reinterpreting it according to
>the Darnell syntax rather than the more logical Brader-Heuer syntax), and by
>the opinion that array copy with compile-time array sizes is not especially
>useful.)

I have a slightly different philosophy than you.  I use C as I can find it.
I use it on a whole bunch of flavors of UN*X and also Turbo C 2.0 at home.
The fact is, X3J11 DID NOT make the changes and C compilers treat the name
of an array as a constant.

Also, this is the way most people view C if they are ex-assembly language
programmers.  At NYU, I teach C as "portable assembly language" with some
success, and that is the way I understand it.

One explanation for why X3J11 did not "fix" the "declaration botch" is 
because it would change the way many systems programmers use C. I believe 
that things in C are the way they are in order to make it as close to
assembly language as possible and still be portable.  Thus something like

	int a[10],b[10];
	...
	a = b;

is easy enough to interpret as array copy, but it be a "high-level" construct
that is not found in other semnatic areas of C.

Before you start flaming me about machine architectures that support 
multi-word copies in one instruction (e.g. 8086 MOVS), remember that C
was developed in the early '70s on a PDP 11 which did not have such
instructions.  I guess that really means that it isn't such a bad idea
to allow array copies now ;-).



More information about the Comp.lang.c mailing list