An error occurred in the sample program of the Chinese Version C Primer Plus Fifth edition, primerplus

Source: Internet
Author: User

An error occurred in the sample program of the Chinese Version C Primer Plus Fifth edition, primerplus

The program with an error occurs on the ListItemCount () and Traverse () Functions on page 1 in Chapter 2.

The original book contains list. c defined by all functions as follows:

 

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include "list. h "4 5 static void CopyToNode (Item item, Node * pnode); 6 7 void InitializeList (List * plist) 8 {9 * plist = NULL; // movie = NULL10} 11 12 bool ListIsEmpty (const List * plist) 13 {14 if (* plist = NULL) 15 return true; 16 else17 return false; 18} 19 20 bool ListIsFull (const List * plist) 21 {22 Node * pt; 23 bool full; 24 pt = (Node *) malloc (sizeof (Node ); 25 if (pt = NULL) 26 full = true; 27 else28 full = false; 29 free (pt); 30 return full; 31} 32 33 34 unsigned int ListItemCount (const List * plist) 35 {36 unsigned int count = 0; 37 Node * pnode = * plist; 38 39 while (pnode! = NULL) 40 {41 + + count; 42 pnode = pnode-> next; 43} 44 return count; 45} 46 47 48 bool AddItem (Item item, List * plist) 49 {50 Node * pnew; 51 Node * scan = * plist; 52 53 pnew = (Node *) malloc (sizeof (Node); 54 if (pnew = NULL) 55 return false; 56 57 CopyToNode (item, pnew); 58 pnew-> next = NULL; 59 if (scan = NULL) 60 * plist = pnew; 61 else62 {63 while (scan-> next! = NULL) 64 scan = scan-> next; 65 scan-> next = pnew; 66} 67 return true; 68} 69 70 71 void Traverse (const List * plist, void (* pfun) (Item item Item) 72 {73 Node * pnode = * plist; 74 while (pnode! = NULL) 75 {76 (* pfun) (pnode-> item); 77 pnode = pnode-> next; 78} 79} 80 81 82 void EmptyTheList (List * plist) 83 {84 Node * psave; 85 while (* plist! = NULL) 86 {87 psave = (* plist)-> next; 88 free (* plist); 89 * plist = psave; 90} 91} 92 93 94 95 static void CopyToNode (Item item, Node * pnode) // static function limits that this function can only use 96 {97 pnode-> item = item; 98} in this file}

 

However, the calling methods in film3.c are as follows:

 

1 Traverse (movies, showmovies); // pass the copy of the movies pointer 2 3 printf ("You entered % d movies. \ n ", ListItemCount (movies); // transmits a copy of the movies pointer.

 

That is to say, when a function is called, the pointer has been passed, but it is used as the pointer address and pointer to the pointer in the function body, which leads to an error.

There are two solutions. One is to change the function call as follows:

 

1 Traverse (& movies, showmovies); // The movies pointer address passed is 2 3 printf ("You entered % d movies. \ n ", ListItemCount (& movies); // The movies pointer address is passed.

 

The second is to change the function bodies of these two functions:

 

 1 unsigned int ListItemCount(const List * plist) 2 { 3     unsigned int count = 0; 4     Node * pnode = plist; 5  6     while(pnode!=NULL) 7     { 8         ++count; 9         pnode = pnode->next;10     }11     return count;12 }13 14 void Traverse(const List * plist,void(*pfun)(Item item))15 {16     Node * pnode = plist;17     while(pnode!=NULL)18     {19         (*pfun)(pnode->item);20         pnode = pnode->next;21     }22 }

 

Both methods can be used. I don't know whether it is the original or the publisher's calibration error. I hope you can see it.

 




Related Article

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.