Michael B Allen <> wrote in message
news

om...
> If I have two unsigned char * and I compare the difference to a size_t
> like:
>
> unsigned char *src, *end;
> size_t bufsiz;
> ...
> if ((end - src) > bufsiz) {
> /* error */
>
> This generates a compiler warning:
>
> warning: comparison between signed and unsigned
>
> What is the correct way to make this comparison?
Define 'bufsiz' as type 'ptrdiff_t'.
>Can I just cast bufsiz
> to ptrdiff_t?
Not with always predictable results.
'size_t' is an unsigned type.
'ptrdiff_t' is a signed type.
If the result of a pointer subtraction is negative,
and you cast this result to an unsigned type (e.g. 'size_t',
you'll get an erroneous value).
-Mike