在 BinarySearchTree 中存储对象会导致奇怪的取消引用问题
Storing object in BinarySearchTree leads to weird dereference problems
对于一项作业,我需要编写将字符串作为输入并计算该字符串中最常用的单词的代码。
我们需要使用名为 "WordCount" 的结构的二叉搜索树来实现这一点,该结构包含一个字符数组和单词出现次数的计数。
结构是这样定义的。
struct wordcount {
char word[80];
int count;
};
typedef struct wordcount WordCount;
二叉搜索树必须有创建节点的方法,这是代码:
BSTnode* createNode(void* item) {
BSTnode* newNode = (BSTnode*) malloc(sizeof(BSTnode));
newNode->item = item;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
当我将 WordCount 结构存储在二叉搜索树中并尝试访问该项目,然后访问该词时,出现了分段错误。
如果我只是尝试访问树的项目,我会得到 char 字数组。这没有意义,因为我正在存储 wordCount 结构,所以我必须取消引用它两次。
int main(int argc, char* argv[]) {
if (argc >= 1) {
WordCount* firstWord = (WordCount*) malloc(sizeof(WordCount));
strcpy(firstWord->word,argv[1]);
firstWord->count = 0;
BSTnode* BST = createNode(firstWord);
printf("%s", BST->item); // should be BST->item->word...but this does not work and says that it cannot find "word" which is apart of
}
/*int i;
char string[80];
for(i = 1; i < argc; i++) {
sscanf(argv[i], "%s", string);
//printf("%s ", string);
//insert(main, argv[i], wordCountCompare);
}*/
}
非常感谢任何帮助。让我知道我的解释是否完全含糊或不完整,或者我是否完全忽略了某些内容。
我还想澄清一下,printf 语句仅用于调试,它们不会与实际程序分开...但是这一点仍然成立。
BST节点的定义:
struct bstnode {
void *item;
struct bstnode *left;
struct bstnode *right;
};
typedef struct bstnode BSTnode;
您可能会遇到这些错误,因为您正在尝试取消引用指向 NULL 的指针。了解如何使用调试器!提示:中断 main 并单步执行函数以查看出现分段错误的位置
printf("%s", BST->item);
Printf 的“%s”需要一个字符串,item
是一个结构。尝试:
printf("%s" BST->item->word);
您的项目应该是wordcount类型。
struct bstnode {
wordcount *item;
struct bstnode *left;
struct bstnode *right;
};
typedef struct bstnode BSTnode;
您不能取消引用 void 指针(这会导致编译错误)
对于一项作业,我需要编写将字符串作为输入并计算该字符串中最常用的单词的代码。
我们需要使用名为 "WordCount" 的结构的二叉搜索树来实现这一点,该结构包含一个字符数组和单词出现次数的计数。
结构是这样定义的。
struct wordcount {
char word[80];
int count;
};
typedef struct wordcount WordCount;
二叉搜索树必须有创建节点的方法,这是代码:
BSTnode* createNode(void* item) {
BSTnode* newNode = (BSTnode*) malloc(sizeof(BSTnode));
newNode->item = item;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
当我将 WordCount 结构存储在二叉搜索树中并尝试访问该项目,然后访问该词时,出现了分段错误。
如果我只是尝试访问树的项目,我会得到 char 字数组。这没有意义,因为我正在存储 wordCount 结构,所以我必须取消引用它两次。
int main(int argc, char* argv[]) {
if (argc >= 1) {
WordCount* firstWord = (WordCount*) malloc(sizeof(WordCount));
strcpy(firstWord->word,argv[1]);
firstWord->count = 0;
BSTnode* BST = createNode(firstWord);
printf("%s", BST->item); // should be BST->item->word...but this does not work and says that it cannot find "word" which is apart of
}
/*int i;
char string[80];
for(i = 1; i < argc; i++) {
sscanf(argv[i], "%s", string);
//printf("%s ", string);
//insert(main, argv[i], wordCountCompare);
}*/
}
非常感谢任何帮助。让我知道我的解释是否完全含糊或不完整,或者我是否完全忽略了某些内容。
我还想澄清一下,printf 语句仅用于调试,它们不会与实际程序分开...但是这一点仍然成立。
BST节点的定义:
struct bstnode {
void *item;
struct bstnode *left;
struct bstnode *right;
};
typedef struct bstnode BSTnode;
您可能会遇到这些错误,因为您正在尝试取消引用指向 NULL 的指针。了解如何使用调试器!提示:中断 main 并单步执行函数以查看出现分段错误的位置
printf("%s", BST->item);
Printf 的“%s”需要一个字符串,item
是一个结构。尝试:
printf("%s" BST->item->word);
您的项目应该是wordcount类型。
struct bstnode {
wordcount *item;
struct bstnode *left;
struct bstnode *right;
};
typedef struct bstnode BSTnode;
您不能取消引用 void 指针(这会导致编译错误)