Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!rpi!batcomputer!cornell!uw-beaver!fluke!ssc-vax!dmg From: dmg@ssc-vax.UUCP (David Geary) Newsgroups: comp.sys.amiga Subject: Re: Struggling through the "C" Message-ID: <2570@ssc-vax.UUCP> Date: 5 Apr 89 23:23:43 GMT Organization: Boeing Aerospace Corp., Seattle WA Lines: 62 In article <97113@sun.Eng.Sun.COM>, Chuck McManis writes: >In article <12000@louie.udel.EDU> (Rob Lizak Jr.) writes: >> FOR I=1 TO LEN(A$) >> PRINT MID$(A$,I,1); >> NEXT I > >> for(i=0;a[i]=!'\0';i++) >> printf("%c",a[i]); >> >>no matter what I put for the declaration... it wouldnt work! I added this >>to the program to try to make it work... whats wrong with this? > >char *a; /* This is a pointer to a string */ >char a[80]; /* this is a pointer to a string with 80 bytes allocated */ No! char a[80] is not a pointer at all. It is an array. Using the name of an array *under certain conditions* is equivalent to a pointer to the first element of the array, but an array is very different from a pointer (one aspect of C that can be quite confusing). Anyway, what Rob is trying to do is perfectly legal. The problem arises in the for loop: for(i=0; a[i]=!`\0`; i++) ^^ This should be: for(i=0; a[i]!=`\0`; i++) ^^ Instead of the conditional part of the loop checking to see if the next char. is NULL, a[i] is ASSIGNED a value. Since '\0' is equivalent to 0 on most systems, we get a[i] = !0. !0 is not false, so it is true. In C, true, (when it's the result of logical operators) is 1. Therefore, Rob is assigning every character in the array the value of 1. Not quite what was intended! Two other notes: Cramming everything together with no spaces is not good style. I would have written the for loop thusly: for(i = 0; a[i] != NULL; i++) This is much more readable, and I suspect it would be easier to spot if the ! and = were in the wrong order. Secondly, this would be more appropriate for comp.lang.c ps: Sorry if someone else has already pointed this out, but I'm way behind on my news reading, and there aren't many questions in this newsgroup that I find I can answer ;-). David Geary -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ David Geary, Boeing Aerospace, ~ ~ #define Seattle RAIN ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~