我遇到了一个返回 Kernighan & Ritchie 的“C 编程语言”结构的示例。
/* binsearch: find word in tab[0]...tab[n-1] */
struct key *binsearch(char *word, struct key *tab, int n)
{
int cond;
struct key *low = &tab[0];
struct key *high = &tab[n];
struct key *mid;
while (low < high) {
mid = low + (high-low) / 2;
if ((cond = strcmp(word, mid->word)) < 0)
high = mid;
else if (cond > 0)
low = mid + 1;
else
return mid;
}
return NULL;
}
似乎该函数正在返回一个指向函数中本地变量的指针;这不是返回悬空指针的情况吗?