Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!inuxc!iuvax!pur-ee!uiucdcs!uxc.cso.uiuc.edu!uicsrd!mcdaniel From: mcdaniel@uicsrd.UUCP Newsgroups: comp.lang.c Subject: Re: Literal strings in C Message-ID: <44200002@uicsrd> Date: Sun, 14-Jun-87 00:37:00 EDT Article-I.D.: uicsrd.44200002 Posted: Sun Jun 14 00:37:00 1987 Date-Received: Wed, 17-Jun-87 00:44:57 EDT References: <212@inco.UUCP> Lines: 42 Nf-ID: #R:inco.UUCP:212:uicsrd:44200002:000:1155 Nf-From: uicsrd.CSRD.UIUC.EDU!mcdaniel Jun 13 23:37:00 1987 > pointing out a couple of real peculiarities. Both of the > following statements are legal, executable C, at least to > the Sun 3.2 C compiler, which is presumably based on PCC. > > c = "literal string"[i]; > > "literal string"[i] = c; It's worse than that. According to K&R, "[]" is not really an operator; it is an abbreviation: a[b] is equivalent to *(a+b) and vice versa. In other words, it's an abbreviation for pointer arithmetic. In C's arithmetic model + is commutative, so a[b] is equivalent to b[a] I just compiled and ran this program: #include main() { int i; i = 5; fprintf(stderr, "%c\n", "0123456789"[i]); fprintf(stderr, "%c\n", "0123456789"[5]); fprintf(stderr, "%c\n", i["0123456789"]); fprintf(stderr, "%c\n", 5["0123456789"]); } and got output of 5 5 5 5 as expected. -- Tim, the Bizarre and Oddly-Dressed Enchanter Center for Supercomputing Research and Development at the University of Illinois at Urbana-Champaign UUCP: {ihnp4,seismo,pur-ee,convex}!uiucdcs!uicsrd!mcdaniel ARPANET: mcdaniel%uicsrd@a.cs.uiuc.edu CSNET: mcdaniel%uicsrd@uiuc.csnet BITNET: mcdaniel@uicsrd.csrd.uiuc.edu