From: David M. <David_McIntosh@ScotiaCapital.com> - 2001-10-24 20:19:31
|
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> The correct line is: <p><tt>im = ib + (ie-ib)/2;</tt> <p>You put in an extra, incorrect, set of parenthesies. Think of the simple numerical example: <br> ib=4; ie=8; <br>You want im=6; Note <br> ib + (ie-ib)/2 = 4 + (8-4)/2 = 4 + (4)/2 = 4 + 2 = 6 <br>but <br> (ib + (ie-ib))/2 = (4 + (8-4))/2 = (4 + (4))/2 = 8/2 = 4 <br>which is wrong. So even if the compiler allowed you to do the calculation you used, it gives the wrong element. In effect, the rules of pointer arithmetic have forced you to do the calculation correctly. <p><font color="#3366FF">> > pointer - pointer => number of elements between the pointers (not bytes)</font> <br><font color="#3366FF">> > pointer + offset => pointer to the element offset after pointer</font> <br><font color="#3366FF">> > all other is not allowed</font> <br><font color="#3366FF">></font> <br><font color="#3366FF">> This is right!</font> <br><font color="#3366FF">></font> <br><font color="#3366FF">> > To get the middle element use something like</font> <br><font color="#3366FF">> > pa + (pe - pa) / 2</font> <br><font color="#3366FF">></font> <br><font color="#3366FF">> i try:</font> <br><font color="#3366FF">> im = (ib + (ie - ib)) / 2;</font> <br><font color="#3366FF">></font> <br><font color="#3366FF">> end get error:</font> <br><font color="#3366FF">> invalid operands `int *' and `int' to binary `operator /'</font> <br><font color="#3366FF">></font> <br><font color="#3366FF">> other ideas to resolve the problem of "/ 2"?</font> <br> <br> </html> |