嗨,这是关于 Kernighan 和 Ritchie 关键字计数程序的问题(ANSI 版的第 6 章第 3 节)。我在下面的链接中包含了整个代码。
当我尝试在任何 C 源代码上运行代码时,我没有得到任何输出。因此,为了查明问题,我在代码的不同点打印了语句。终端窗口中程序的输出(应用到自身时)现在看起来像这样:
./a.out < keywords.c
I've got past the beginning of the getword loop.
I've got past the beginning of the getword loop.
I'm past the word[0] condition.
Segmentation fault
当我使用另一种搜索方法(通过结构键数组进行线性搜索)时,我得到了相同输出的另一种组合,这次没有分段错误。根据 printf 语句的输出,我倾向于认为 getword 函数有问题。那么是什么导致了这些错误呢?
以下是具有二进制和线性搜索功能的完整代码: