sbigham@dukeac.UUCP (Scott Bigham) (11/03/88)
In article <10742@cup.portal.com> ts@cup.portal.com (Tim W Smith) writes: >I don't know about i[ptr] being less readable than ptr[i]. Most people I >have heard would say when describing an algorithm in English, "Then you take >the ith element of the array and multiply by foo". i[array]*foo has the >words in C in the same order they would be spoken. a[b] looks like an array reference, so, naive programmer that I am, I assume that it _is_ an array reference. In particular, I assume that a is an array and b is an index into that array. Something that does otherwise is less readable to me, and I suspect I'm not alone. The point is, is there ever any practical reason to use this construct? Do you ever use it? Does anyone? sbigham -- Scott Bigham "The opinions expressed above represent Internet sbigham@dukeac.ac.duke.edu me and everyone that agrees with me. USENET sbigham@dukeac.UUCP If that includes Duke University, ...!mcnc!ecsgate!dukeac!sbigham I'll be amazed."
rbutterworth@watmath.waterloo.edu (Ray Butterworth) (11/08/88)
In article <1076@dukeac.UUCP>, sbigham@dukeac.UUCP (Scott Bigham) writes: > a[b] looks like an array reference, so, naive programmer that I am, I assume > that it _is_ an array reference. In particular, I assume that a is an array > and b is an index into that array. Something that does otherwise is less > readable to me, and I suspect I'm not alone. > > The point is, is there ever any practical reason to use this construct? Do > you ever use it? Does anyone? One use I've seen is to have: extern int **pointer; index[*pointer] instead of: (*pointer)[index] It saves two keystrokes :-). Another, slightly more reasonable, is in treating an array like a structure and the [] like function or macro invocation: typedef int Date[6]; #define YEAR 0 #define MONTH 1 ... extern Date table[3]; YEAR[table[2]] = 1986; is slightly more readable than: table[2][YEAR] = 1986; Of course it would probably be better to: #define YEAR(x) (x)[0] YEAR(table[2]) = 1986; or even better to: typedef struct { int year; int month; ... } Date; table[2].year = 1986; but code that used the YEAR[table[2]] form tended to be ported from B, which has neither macros nor structures. In any case, the compiler should generate identical code for all of these. It's simply a matter of style, and while there are many wrong styles, there really isn't any one right style.
tps@chem.ucsd.edu (Tom Stockfisch) (11/08/88)
So how come
int 5[a], 5[*b];
isn't a legal declaration equivalent to
int a[5], (*b)[5];
and the following isn't a legal cast?
(int 10[*])
--
|| Tom Stockfisch, UCSD Chemistry tps@chem.ucsd.edu
chris@mimsy.UUCP (Chris Torek) (11/10/88)
In article <351@chem.ucsd.EDU> tps@chem.ucsd.edu (Tom Stockfisch) writes: [compressed vertically] >So how come > int 5[a], 5[*b]; >isn't a legal declaration equivalent to > int a[5], (*b)[5]; >and the following isn't a legal cast? > (int 10[*]) Because the `declaration mirrors use' rule is only an approximation. (Tom was no doubt asking rhetorically, but no doubt someone will misunderstand.) Mainly, it breaks down with array declarations and use. This is probably the cause of most of the confusion as to why code like /* file1.c */ char text[] = "foo the bar"; /* file2.c */ extern char *text; /* ... code referencing *(text+n) ... */ fails. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
mouse@mcgill-vision.UUCP (der Mouse) (11/15/88)
In article <1076@dukeac.UUCP>, sbigham@dukeac.UUCP (Scott Bigham) writes: > In article <10742@cup.portal.com> ts@cup.portal.com (Tim W Smith) writes: >> I don't know about i[ptr] being less readable than ptr[i]. [...] > a[b] looks like an array reference, so [...] I assume that it _is_. > The point is, is there ever any practical reason to use this > construct? Do you ever use it? Does anyone? "Practical" is doubtful, but since nobody else has mentioned this.... main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);} So there. :-) der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu (No, that program isn't original with me, sorry.)