我正在学习本书,第二版。我在下面的书中编写了代码,并对其进行了编译,但是它不起作用。我的终端上显示“ Segamentation fault:11”消息。
上次我遇到了类似的问题,我通过在数组末尾添加'\ 0'来解决了这个问题。但是这次我不知道问题是从哪里开始的。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORD 100
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);
char *strdupH(char *);
void treeprint(struct tnode *);
struct key {
char *word;
int count;
};
struct key keytab[] = {
"auto", 0,
"break", 0,
"case", 0,
"char", 0,
"const", 0,
"continue", 0,
"default", 0,
"unsigned", 0,
"void", 0,
"volatile", 0,
"while", 0
};
struct tnode { /* the tree node */
char *word; /* points to the text */
int count; /* number of occurences */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
};
// It is illegal for a structure to contain an instance of itself.
// struct tnode *left
// declares left to be a pointer to a tnode, not tnode itself
//
// 6_5SelfReferentialStructures.c
//
//
// Created by Damian on 26/05/2019.
//
/* getword: get next word or character from input */
int getword(char *word, int lim)
{
int c, getchH(void);
void ungetchH(int);
char *w = word;
while (isspace(c = getchH()))
;
if (c != EOF)
*w++ = c;
if (!isalpha(c)) {
*w = '\0';
return c;
}
for ( ; --lim > 0; w++)
if (!isalnum(*w = getchH())) {
ungetchH(*w);
break;
}
*w = '\0';
return word[0];
}
int getchH(void) /* get a (possibly pushed back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetchH(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
/* word frequency count */
int main(int argc, char *argv[])
{
struct tnode *root;
char word[MAXWORD];
root = NULL;
while (getword(word, MAXWORD) != EOF)
if (isalpha(word[0])) {
root = addtree(root, word);
}
treeprint(root);
return 0;
}
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
int cond;
if ((p = NULL)) {
p = talloc(); /* make a new node */
p->word = strdupH(w);
p->count = 1;
p->left = p->right = NULL;
} else if ((cond = strcmp(w, p->word)) == 0)
p->count++; /* repeated word */
else if (cond < 0) /* less than into left subtree */
p->left = addtree(p->left, w);
else /* greater than into right substree */
p->right = addtree(p->right, w);
return p;
}
/* treeprint: in-order print of tree p */
void treeprint(struct tnode *p)
{
if (!p) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
return;
}
/* talloc: make a tnode */
struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
}
char *strdupH(char *s) /* make a duplicate of s */
{
char *p;
p = (char *) malloc(strlen(s) + 1); /* +1 for '\0' */
if (p != NULL)
strcpy(p, s);
return p;
}
以上代码的结果为:
damian@James:~/t2/t2$ cc main.c -o main
damian@James:~/t2/t2$ ./main
Can you help me?
Segmentation fault: 11
感谢您的时间。
答案 0 :(得分:2)
您的细分错误来自线路
if ((p = NULL))
应该是
if (p == NULL)
答案 1 :(得分:2)
以下是错误的地方
if
中的addtree
语句不正确。将if ((p = NULL))
更改为if ((p == NULL))
在talloc
函数中,应使用left
初始化right
和NULL
struct tnode *talloc(void)
{
struct tnode *newnode;
newnode = malloc(sizeof(struct tnode));
newnode -> left = NULL;
newnode -> right = NULL;
return (newnode);
}
treeprint
函数应为if(p)
而不是if(!p)
void treeprint(struct tnode *p)
{
if (p) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
return;
}
您应该使用花括号来初始化keytab
数组。
struct key keytab[] = {
{"auto", 0,},
{"break", 0,},
//etc
};