错误的程序出现再第17章的499页ListItemCount()和500页的Traverse()两个函数上。
原著包含所有函数定义的list.c如下:
#include<stdio.h>
#include<stdlib.h>
#include "list.h" static void CopyToNode(Item item,Node * pnode); void InitializeList(List * plist)
{
*plist = NULL;//movie = NULL
} bool ListIsEmpty(const List * plist)
{
if(*plist==NULL)
return true;
else
return false;
} bool ListIsFull(const List * plist)
{
Node * pt;
bool full;
pt = (Node *)malloc(sizeof(Node));
if(pt==NULL)
full = true;
else
full = false;
free(pt);
return full;
} unsigned int ListItemCount(const List * plist)
{
unsigned int count = ;
Node * pnode = *plist; while(pnode!=NULL)
{
++count;
pnode = pnode->next;
}
return count;
} bool AddItem(Item item,List *plist)
{
Node * pnew;
Node * scan = *plist; pnew = (Node *)malloc(sizeof(Node));
if(pnew == NULL)
return false; CopyToNode(item,pnew);
pnew->next = NULL;
if(scan==NULL)
*plist = pnew;
else
{
while(scan->next!=NULL)
scan = scan->next;
scan->next = pnew;
}
return true;
} void Traverse(const List * plist,void(*pfun)(Item item))
{
Node * pnode = *plist;
while(pnode!=NULL)
{
(*pfun)(pnode->item);
pnode = pnode->next;
}
} void EmptyTheList(List * plist)
{
Node * psave;
while(*plist!=NULL)
{
psave = (*plist)->next;
free(*plist);
*plist = psave;
}
} static void CopyToNode(Item item,Node * pnode)//静态函数限制该函数只能在本文件内使用
{
pnode->item = item;
}
但是在film3.c中调用的形式分别如下:
Traverse(movies,showmovies); //传递的是movies指针的拷贝 printf("You entered %d movies.\n",ListItemCount(movies)); //传递的也是movies指针的拷贝
也就是说,函数调用的时候已经传递的是指针了,但函数体内调用时却又把它当成指针的地址,及指向指针的指针,从而发生错误
解决的方法有两个,一是更改函数调用如下:
Traverse(&movies,showmovies); //传递的是movies指针的地址 printf("You entered %d movies.\n",ListItemCount(&movies));//传递的也是movies指针的地址
二是更改这两个函数的函数体:
unsigned int ListItemCount(const List * plist)
{
unsigned int count = ;
Node * pnode = plist; while(pnode!=NULL)
{
++count;
pnode = pnode->next;
}
return count;
} void Traverse(const List * plist,void(*pfun)(Item item))
{
Node * pnode = plist;
while(pnode!=NULL)
{
(*pfun)(pnode->item);
pnode = pnode->next;
}
}
总之两个方法都可以,不知道是原著的错误,还是出版社校准的错误,希望大家能看出来。