Win10
gcc
goo.gl/Q3ybkG
p145,146
大意就是拿一个字串跟结构阵列里面的每个字串成员做比较,找到一样的字串
结构阵列的字串已经是由小到大的排列了
int binsearch(char *word, struct key tab[], int n)
n是阵列大小,在binsearch里面
low = 0;
high = n - 1;
后来又改成一个指标指到结构版本 p148,149
struct key *low = &tab[0];
struct key *high = &tab[n];
请问为什么这边的n就不减一了?
后面写到
The initializers for low and high are now pointers to the beginning and just
past the end of the table(阵列结尾的下一位)
mid = (low+high)/2 /* WRONG */
because the addition of pointers is illegal. Subtraction is legal, however,
so high-low is the number of elements, and thus
mid = low + (high-low)/2
应该只是说指标不能相加,但是可以相减,虽然第二种也可以避免overflow的问题
但还是不懂为什么n不减一
谢谢