Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
int main( void ) {
int foo[10], *bar=foo, *baz=foo+1;
int quux=baz-bar;
return 0;
}
Is the type of quux (that is, int) strictly correct? Or is there a
type similar to size_t that is more appropriate for the difference of
two pointers?
ptrdiff_t is correct (as Jordan Abel has already pointed out). On the
other hand, that doesn't mean the code above is wrong. As long as you
know the result won't exceed 32767, there's nothing wrong with
assigning it to an int and taking advantage of the implicit
conversion.
But declaring quux as a prtdiff_t is more robust.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.