I am allocating memory for a sting of text and then picking out a word at a time. I am getting stuck on the for loop. I am trying to break a for loop and am having a hard time. I am new to programming and the c language. I need some advice on this one. I have attached the full code. I have tried this break and the program shuts down. if((pList->Word)='\0') break; Code: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #define MAXWORDS 1000 typedef struct { char Word[88]; }UniqueWords; char *GetWord(char *Source, char *Dest); int main(int c, char **v) { char *LocationGetty = "C:\\Users\\svjefcoat\\Documents\\Visual Studio 2010\\Projects\\assignment 10\\Getty.txt"; UniqueWords Word[MAXWORDS]; UniqueWords *pList; char line[99]; char line2[99]; char Dest[39]; char Dest2[39]; char *Read; FILE *pStorage; char *pDArray; int size; pStorage = fopen(LocationGetty, "r"); //Location Getty if(!pStorage) return 0; size = (sizeof(pStorage)); while(1) { if(feof(pStorage)) break; *line=NULL; fgets(line,sizeof(line), pStorage); pDArray = malloc(sizeof(line)); strcpy(pDArray, line); pDArray[(sizeof(line))+1] = '\0'; printf("%-s", line); Read = pDArray; pList = Word; for(;;) { Read = GetWord(Read, pList->Word); pList++; } free(pDArray); getchar(); return 0; } //pList++; fclose(pStorage); getchar(); return 0; } char *GetWord(char *Source, char *Dest) { // skip all leading space characters while(*Source) { if(isspace(*Source)) { Source++; continue; } else break; } while(*Source) { if(isspace(*Source)) { *Dest = '\0'; return Source; } *Dest++ = *Source++; } *Dest = '\0'; return Source; }
If you are trying to break this for loop Code: for(;;) { Read = GetWord(Read, pList->Word); pList++; } then the program should exit because there is nothing much to be executed after this. Unconditional return 0 inside a while loop means the loop will be executed only once.
if you need another way to break the look then use some condition corresponding to a “break” and “exit(1)”…
I have tried this break and the program shuts down. if((pList->Word)='\0') break; here is nothing a small corection for assigning 5 to 'a' then we single '=' i.e like " a=5 " if we want campare 'a' with 5 then we use '==' i.e like if(a==5) pf("pass"); in above if((pList->word)=='\0')