Error 1 in the Chinese version of C Primer Plus (Sixth edition)

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineTsize 455 6 structFilm {7     CharTitle[tsize];8     intrating;9     structFilm *Next;Ten }; One Char* S_gets (Char* St,intn); A  - intMainvoid) - { the     structFilm * head =NULL; -     structFilm *prev; -     structFilm *Current ; -     CharInput[tsize]; +  -Puts"Enter First movie title:"); +      while(S_gets (input, tsize)! = NULL && input[0] !=' /') A     { atCurrent = (structFilm *)malloc(sizeof(structfilm)); -         if(Head = =NULL) -Head =Current ; -         Else -Prev->next =Current ; -Current->next =NULL; instrcpy (current->title, input); -Puts"Enter your rating <0-10>:"); toscanf"%d", &current->rating); +          while(GetChar ()! ='\ n') -             Continue; thePuts"Enter Next movie title (empty line to stop):"); *Prev =Current ; $     }Panax Notoginseng  -     if(Head = =NULL) theprintf"No data entered."); +     Else Aprintf"Here is the movie list:\n"); theCurrent =head; +      while(Current! =NULL) -     { $printf"Movie:%s Rating:%d\n", $Current->title, current->rating); -Current = Current->Next; -     } the  -Current =head;Wuyi      while(Current! =NULL) the     { -Current =head; WuHead = current->Next; -          Free(current); About     } $printf"bye!\n"); -  -     return 0; - } A  + Char* S_gets (Char* St,intN) the { -     Char*Ret_val; $     Char*find; the  theRet_val =Fgets (St, N, stdin); the     if(Ret_val) the     { -Find = STRCHR (St,'\ n'); in         if(Find) the*find =' /'; the         Else About              while(GetChar ()! ='\ n') the                 Continue; the     } the  +     returnRet_val; -}
View Code

The above code is from "C Primer Plus" (sixth edition) Chinese version, 第573-574 page.

There is an error in line 51st of the code.

I do not know whether the error is printing wrong, or the original book is wrong. Noted here.

Correction Code:

is to change the while of line 51st (current! = NULL) to

while (head! = NULL) is ready.

The reasons are as follows:

Add some output between the 第51-56 line code as follows:

1      while(Current! =NULL)2     {3printf"current1 =%p\n", current);4printf"head1 =%p\n\n", head);5Current =head;6printf"Current2 =%p\n", current);7printf"head2 =%p\n\n", head);8Head = current->Next;9printf"Current3 =%p\n", current);Tenprintf"head3 =%p\n\n", head); One          Free(current); Aprintf"current4 =%p\n", current); -printf"head4 =%p\n\n", head); -}
View Code

The output results are as follows:

As you can see from the above output, the last head2 pointer is already 00000000 and is a pointer to an unknown address. So releasing a current again will make a mistake.

Corrected output:

Error 1 in the Chinese version of C Primer Plus (Sixth edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.