我正在尝试使用二进制搜索树按字母顺序排列一些单词。下面是代码的一部分:为什么我会在这方面发生分割错误?
int wordCmp (char a[], char b[])
{
int i = 0;
while(a[i] == b[i])
++i;
return a[i] < b[i];
}
void Insert (node* &root , char a[])
{
if(root == NULL)
root = CreateNewNodde(a);
else
{
if (wordCmp(a,root -> word))
Insert(root -> left , a);
else Insert(root -> right, a);
}
}
我得到thise指令分段错误:
int wordCmp (char a[], char b[])
{
int i = 0;
- > while(a[i] == b[i]) <- Segmentation Fault
++i;
return a[i] < b[i];
}
'while(a [i] && a [i] == b [i]) –
谢谢,这似乎是问题所在。 – ivanciprian
'char *'不是表示“单词”的好方法。使用'std :: string'。 –