Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!amdahl!kevin From: kevin@uts.amdahl.com (Kevin Clague) Newsgroups: comp.sys.amiga Subject: Re: Struggling through the "C" Message-ID: <13.6va2CVf1010TxC9.@amdahl.uts.amdahl.com> Date: 1 Apr 89 12:21:57 GMT References: <12000@louie.udel.EDU> <97113@sun.Eng.Sun.COM> Reply-To: kevin@amdahl.uts.amdahl.com (Kevin Clague) Organization: Amdahl Corporation, Sunnyvale, CA 94086 Lines: 73 In article <97113@sun.Eng.Sun.COM> cmcmanis@sun.UUCP (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 > >C equivalent might be more syntactically written as > for (i=0; i < strlen(a); i++) > printf("%c", a[i]); > >Get a copy of the book "Programming in C" by Stephen G. Kochan, ISBN >0-8104-6261-3, it has a good discussion of strings. > >>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 */ > >Outside of a function you can declare : >char *a = "this is a test string."; /* the auto allocates space */ >-or- >char a[80]; /* Global variable */ >main() >{ > char b[80]; /* Local variable */ > /* this copies a string into your string buffer a */ > strcpy(a,"this is a test string"); > strcpy(b,a); /* Copy string a into string b */ > >} >----- > >--Chuck McManis >uucp: {anywhere}!sun!cmcmanis BIX: cmcmanis ARPAnet: cmcmanis@sun.com >These opinions are my own and no one elses, but you knew that didn't you. >"A most excellent barbarian ... Genghis Kahn!" I had a hard time understanding from the posting at what level "things didn't work". I presumed Rob had gotten the thing to compile, and the problem was with "buffered I/O". Since Rob had no newline characters in his test string (and it was so short), the I/O buffers would not be flushed, and therefore he would get no output. I sent email suggesting this as a possible explanation. To really be compatible with basic (since he had no newlines), I think he would have to fflush(stdout) after every printf(), no? for (i = 0; i < strlen(a); i++) { printf("%c",a[i]); fflush(stdout); } or the less obvious, but more efficient (but Chuck knows this): char *b; b = a; while (*b) { printf("%b",*b++); fflush(stdout); } Kevin -- UUCP: kevin@uts.amdahl.com or: {sun,decwrl,hplabs,pyramid,seismo,oliveb}!amdahl!kevin DDD: 408-737-5481 USPS: Amdahl Corp. M/S 249, 1250 E. Arques Av, Sunnyvale, CA 94086 [ Any thoughts or opinions which may or may not have been expressed ] [ herein are my own. They are not necessarily those of my employer. ]