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不減一
謝謝