Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!udel!brahms.udel.edu!stas From: stas@brahms.udel.edu (Stanislaus Pietrucha) Newsgroups: comp.sys.mac.programmer Subject: Think C bug Message-ID: <21514@brahms.udel.edu> Date: 19 May 91 20:42:08 GMT Distribution: usa Organization: University of Delaware Lines: 92 This is the weirdest I've ever seen, and I know it is a compiler error. First of all I compiled the code in Think C and, what you see here works. The problem is the field in the record struct, int dummy. If i try to run this code without the dummy field, it will crash whenever I reference char last[24] (as i do in fgets(...p_current->last)). If this isn't weird enough, if I switch the order of the fields in the record, the error (bad field) is always the same. i.e. if i moved first where last is then i could access last, but not first. It's like a black hole. Without the dummy variable exactly where it is, whatever item (it seems) that is in the 5th position is inaccessable. What the heck is going on???? Take a look at the code: Here is the record structure: struct customer_record { struct customer_record *prev; struct customer_record *next; char number[8]; char first[20]; int dummy; char last[24]; char position[8]; char team[40]; char rookie[8]; char sale[8]; int quantity; }; here is the function in which the program blows up (when dummy not used) int file_to_list(FILE *datafile,struct customer_record *llist) { char last[24],file_header[40]; struct customer_record *p_current; int c, x, done, length = 0; int quantity; p_current = (struct customer_record *) malloc(sizeof (struct customer_record)); llist = p_current; p_current->prev = llist; fgets(file_header,40,datafile); printf("%s\n",file_header); while((c=getc(datafile)) != EOF) { ungetc(c,datafile); fgets(p_current->number,8,datafile); fgets(p_current->first,20,datafile); fgets(p_current->last,24,datafile); fgets(p_current->position,8,datafile); fgets(p_current->team,40,datafile); fgets(p_current->rookie,8,datafile); fgets(p_current->sale,8,datafile); fscanf(datafile,"%d\n",p_current->quantity); p_current->next = (struct customer_record *) malloc(sizeof (struct customer_record)); p_current->next->prev = p_current; p_current = p_current->next; } fclose(datafile); load_flag = 1; } --------------------And here is the data file: Stan's Client List /*file_header*/ 1 /*number*/ Terri /*first*/ Wache /*last*/ HM /*postition*/ Nashua Hornets /*team*/ Y /*rookie*/ N /*sale*/ 5 /*quantity*/ 2 Carol Veach MG Philadelphia Waxers N N 6 -------------That's it (The comments are for you, i.e. they are not actually in the data file) Don't try to make sense of the data, it's personal. Thanks for any help/input you can give me. Stas