argc[argv]

Morris Keesan keesan at bbncca.ARPA
Wed Dec 21 02:32:10 AEST 1983


---------------------------------
>>>>Beginners may have trouble with the statement that a[b] is entirely
>>>>equivalent to *(a+b).  This is only true for character arrays.  For
>>>>other types of arrays the translation is *(a+b*sizeof(*a)).
>>>>
>>>>					    -- Ken Laws
-------
    Wrong.  Even going by the assumption here that a is an array, then given
the declaration "foo a[];" the expression a[b] is equivalent to *(a+b), and
not *(a+b*sizeof(*a)), because the the scaling of b is done implicitly by the
addition operator -- see the C Reference Manual, section 7.4 (p. 188 of K&R),
which says in the case (pointer + int) "the latter is . . . [multiplied by]
the length of the object to which the pointer points."  Ken Laws is assuming
integer addition, which is clearly not the case.  What he means is that
a[b] is equal to *(foo *)((int)a + b*sizeof(*a)), which is indeed equivalent
to *(a+b).
    As several other people have pointed out, argc[argv] is indeed equivalent
to argv[arc] and (argv+argc) and (argc+argv).  The key to the confusion is
brought out by the 5th paragraph of Section 7.1 of the reference manual
(P. 186, par. 3 of K&R), which states that (primary expression)[expression]
"is a primary expression.  The INTUITIVE meaning is that of a subscript.
USUALLY, the primary expression has type "pointer to ...", the subscript
expression is int, and the type of the result is "...".  The expression
E1[E2] is identical (by definition) to *((E1)+(E2))."  [caps mine]. The 
other useful section to read is section 14.3 (p. 210 of K&R), which
repeats the definition of E1[E2], and points out that "despite its
assymetric appearance, subscripting is a commutative operation."
-- 
					Morris M. Keesan
					{decvax,linus,wjh12}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA



More information about the Comp.unix mailing list