我正在尝试使用此函数将节点添加到树中。我使用字符指针数组作为输入数据。为了插入,我每次都会增加指针。
node addToTree(node root , char *words[])
{
int count=0;
while( *(words+2))
{
printf("\n VALLED %d",count++);
root=addNode(root,*words);
printf("\n the current word is %s",*words);
words++;
}
return root;
}
为什么循环条件中words+2有效,而words或words+1无效
这是单词数组
char *words[3]={"3","8","1"};
最佳答案
您正在迭代 words
中的指针并检查是否为零
指针,但您没有在数组末尾添加哨兵。试试这个:
char *words[] = {"3", "8", "1", NULL};
...
while (*words)
大概,words + 2
起作用是因为碰巧有一些东西
该位置的内存相当于 NULL
指针。
关于c - 指针数组 C - NULL POSITION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24473443/