超长整数

c指针的问题

dcy posted @ 2009年5月31日 08:37 in c , 1327 阅读

两个指针之间的加法是违法的,但是,指针的减法运算却是合法的。
比如low和high的初值分别是指向表头元素的指针和和指向表尾元素后面的一个元素的指针
mid=(low+high)/2                              /*错误*/
因为指针相加没有意义,减法合法,所以我们可以用减法,high-low就是数组元素的个数
mid=low+(high-low)/2

Emma 说:
2023年1月31日 20:12

It is important to remember that pointer addition is not a legal operation; however, pointer subtraction is, and this can be used to calculate the number of array elements. An example of arthritis this can be seen in the following code snippet, which is incorrect due to the pointer addition: mid=(low+high)/2. To correctly calculate the number of array elements, one should use the following: mid=low+(high-low)/2.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter