Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Comparison of ptrdiff_t

Thread Tools

Re: Comparison of ptrdiff_t

Mike Wahler
Posts: n/a

Michael B Allen <(E-Mail Removed)> wrote in message
news(E-Mail Removed) 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).


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
c pointers (ptrdiff_t) andy_dufresne C Programming 1 12-01-2005 10:40 PM
c pointers (ptrdiff_t) andy_dufresne C Programming 3 12-01-2005 02:52 PM
question posted on another forum (ptrdiff_t) C Programming 2 11-30-2005 09:39 PM
ptrdiff_t Stefan Ram C Programming 13 12-18-2004 06:14 PM
Re: Comparison of ptrdiff_t pete C Programming 18 07-10-2003 12:53 PM