Example Program: a[i]=b[i++] non-portability
Joseph S. D. Yao
jsdy at hadron.UUCP
Tue Apr 8 15:27:02 AEST 1986
In article <359 at g.cs.cmu.edu> ckk at g.cs.cmu.edu (Chris Koenigsberg) writes:
>#include <stdio.h>
>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?
More information about the Comp.lang.c
mailing list