Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site bbncca.ARPA Path: utzoo!linus!bbncca!keesan From: keesan@bbncca.ARPA (Morris Keesan) Newsgroups: net.unix Subject: Re: argc[argv] Message-ID: <407@bbncca.ARPA> Date: Tue, 20-Dec-83 11:32:10 EST Article-I.D.: bbncca.407 Posted: Tue Dec 20 11:32:10 1983 Date-Received: Wed, 21-Dec-83 02:02:38 EST References: <14723@sri-arpa.UUCP> Organization: Bolt, Beranek and Newman, Cambridge, Ma. Lines: 31 --------------------------------- >>>>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