Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 09 Jun 2012 06:28:15 -0500 Message-ID: <4FD33350.63FA@mindspring.com> Date: Sat, 09 Jun 2012 07:28:16 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: linked list References: <4FD32A2F.1A9E@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 157 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.219.136 X-Trace: sv3-xHpUX8XDPCVHy3pQ/dR2rv+dz2ywIbVMBGIQep5RFx9h7sCldQOjgMizWvGz6cdA0JCEEJTR1UtrfUt!1VKnWMD9x/eFQxObT7O90wzjos/LlXHR9M4hGOq26tNSqk1xXNF3dzweWBMc4z0QOyTt6OzrWSsy!JF+ScI4zfD8= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4587 Xref: csiph.com comp.lang.c:22058 pete wrote: > > Zhang Yuan wrote: > > > > ---------------------------------------------------------------------------- > > #include > > #include > > struct stu{ > > int num; > > char name[12]; > > int age[0]; > > struct stu *next; > > }; > > > > int main(){ > > > > struct stu *curr,*head; > > > > head=(stu *)malloc(sizeof(stu)); > > curr=head; > > scanf("%d",&(head->num)); > > curr->next=NULL; > > > > while(curr->num!=0){ > > scanf("%s%d",curr->name,curr->age); > > curr=(stu *)malloc(sizeof(struct stu)); > > scanf("%d",&(curr->num)); > > curr->next=NULL; > > } > > return 0; > > } > > > > -------------------------------------------------------------------------- > > it doesn't work.When I debug: > > > > input : > > 101 zhang 19 > > > > the memory information:4 pictures > > http://flic.kr/p/ccMQey > > > > why 0x6f17e0 change from 00-->13 unexpectedly. > > I think the "*next" and "int age"storaged in different place. > > but it looks like it overwrite? > > #include > #include > > struct stu{ > struct stu *next; > int num; > char name[12]; > int age; > }; > > int > main(void) > { > struct stu *curr,*head; > > head = malloc(sizeof *head); > if (head != NULL) { > curr = head; > puts("enter number"); > scanf("%d", &(head -> num)); > curr -> next = NULL; > while(curr -> num != 0){ > puts("enter name and age"); > scanf("%s%d", curr -> name, &(curr -> age)); > curr = malloc(sizeof *curr); That doesn't make any sense! /* BEGIN new.c */ #include #include struct stu{ struct stu *next; int num; char name[12]; int age; }; void stu_free(struct stu *head); void stu_print(struct stu *head); int main(void) { struct stu *curr,*head; puts("/* BEGIN new.c */ output\n"); head = malloc(sizeof *head); if (head != NULL) { curr = head; curr -> next = NULL; puts("enter number"); if (1 > scanf("%d", &(head -> num))) { puts("\n\nscanf problem.\n\n"); exit(EXIT_FAILURE); } while(curr -> num != 0) { puts("enter name and age"); scanf("%s%d", curr -> name, &(curr -> age)); if (1 > scanf("%d", &(head -> num))) { puts("\n\nscanf problem.\n\n"); exit(EXIT_FAILURE); } curr -> next= malloc(sizeof *(curr -> next)); if (curr -> next == NULL) { break; } curr = curr -> next; curr -> next = NULL; puts("enter number"); scanf("%d", &(curr -> num)); } *(curr -> name) = '\0'; curr -> age = 0; } stu_print(head); stu_free(head); puts("\n/* END new.c */ output"); return 0; } void stu_free(struct stu *head) { struct stu *next; while (head != NULL) { next = head -> next; free(head); head = next; } } void stu_print(struct stu *head) { while (head != NULL) { printf("\nnumber == %d\n", head -> num); printf("name is %s\n", head -> name); printf("age is %d\n", head -> age); head = head -> next; } } /* END new.c */ -- pete