Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

## Re: [Mingw-users] pointer

 Re: [Mingw-users] pointer From: David McIntosh - 2001-10-24 20:19:31 ``` The correct line is:

im = ib + (ie-ib)/2;

You put in an extra, incorrect, set of parenthesies.  Think of the simple numerical example:
ib=4; ie=8;
You want im=6;  Note
ib + (ie-ib)/2 = 4 + (8-4)/2 = 4 + (4)/2 = 4 + 2 = 6
but
(ib + (ie-ib))/2 = (4 + (8-4))/2 = (4 + (4))/2 = 8/2 = 4
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.

> > pointer - pointer => number of elements between the pointers (not bytes)
> > pointer + offset => pointer to the element offset after pointer
> > all other is not allowed
>
> This is right!
>
> > To get the middle element use something like
> > pa + (pe - pa) / 2
>
> i try:
>     im = (ib + (ie - ib)) / 2;
>
>  end get error:
>     invalid operands `int *' and `int' to binary `operator /'
>
> other ideas to resolve the problem of "/ 2"?

```

 Re: [Mingw-users] pointer From: David McIntosh - 2001-10-24 20:19:31 ``` The correct line is:

im = ib + (ie-ib)/2;

You put in an extra, incorrect, set of parenthesies.  Think of the simple numerical example:
ib=4; ie=8;
You want im=6;  Note
ib + (ie-ib)/2 = 4 + (8-4)/2 = 4 + (4)/2 = 4 + 2 = 6
but
(ib + (ie-ib))/2 = (4 + (8-4))/2 = (4 + (4))/2 = 8/2 = 4
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.

> > pointer - pointer => number of elements between the pointers (not bytes)
> > pointer + offset => pointer to the element offset after pointer
> > all other is not allowed
>
> This is right!
>
> > To get the middle element use something like
> > pa + (pe - pa) / 2
>
> i try:
>     im = (ib + (ie - ib)) / 2;
>
>  end get error:
>     invalid operands `int *' and `int' to binary `operator /'
>
> other ideas to resolve the problem of "/ 2"?

```