Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!petrus!scherzo!allegra!mit-eddie!think!harvard!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: Example Program: a[i]=b[i++] non-portability Message-ID: <362@hadron.UUCP> Date: Tue, 8-Apr-86 00:27:02 EST Article-I.D.: hadron.362 Posted: Tue Apr 8 00:27:02 1986 Date-Received: Thu, 10-Apr-86 01:21:11 EST References: <359@g.cs.cmu.edu> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 31 Summary: This code suffers! In article <359@g.cs.cmu.edu> ckk@g.cs.cmu.edu (Chris Koenigsberg) writes: >#include >main(argc, argv) > {char a[10];static char b[]="abcdefghij";int i,j; > printf("a =%s, b=%s\n", a, b); > printf("Looping over i..."); > while(b[i] != 0) a[i] = b[i++]; > printf("New a ="); > for(i=0;i<=10;printf("%c",a[i++]));printf(", b=%s\n",b); > fflush(stdout); > } >On a sun, the output looks like: >a =, b=abcdefghij >Looping over i...New a =abcdefghij^@, b=abcdefghij >And on the IBM RT PC, the output looks like: >a =, b=abcdefghij >Looping over i...New a =^@abcdefghij, b=abcdefghij >Note: The "^@" was actually a null character, in the output. >On the sun, a[0] gets b[0], but on the RT PC, a[1] gets b[0] and a[0] is >a null. Chris, run lint on this. One problem is that I IS NOT INITIALISED! Then, you index into a[10] both in the assignment and in the printing loops. Elements a[0-9] exist: a[10] is an addressing error. On the 8086, what model was this compiled under? You were lucky that this ran. (Yes, if core is zeroed first i will always be 0. What happens when you try to make this code a re-entrant subroutine?) As for the main point, this is working OK: the code does what people were saying it should do. I don't have your original message here: is this r e a l l y exactly what got folk upset?