Re: Comparison of ptrdiff_t

Discussion in 'C Programming' started by Mike Wahler, Jul 5, 2003.

  1. Mike Wahler

    Mike Wahler Guest

    Michael B Allen <> wrote in message
    news:p...
    > 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
    Mike Wahler, Jul 5, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pete

    Re: Comparison of ptrdiff_t

    pete, Jul 5, 2003, in forum: C Programming
    Replies:
    18
    Views:
    593
    Finny Merrill
    Jul 10, 2003
  2. Stefan Ram

    ptrdiff_t

    Stefan Ram, Dec 3, 2004, in forum: C Programming
    Replies:
    13
    Views:
    876
    Stefan Ram
    Dec 18, 2004
  3. Replies:
    2
    Views:
    296
    Clark S. Cox III
    Nov 30, 2005
  4. andy_dufresne

    c pointers (ptrdiff_t)

    andy_dufresne, Nov 30, 2005, in forum: C Programming
    Replies:
    3
    Views:
    305
    andy_dufresne
    Dec 1, 2005
  5. andy_dufresne

    c pointers (ptrdiff_t)

    andy_dufresne, Dec 1, 2005, in forum: C Programming
    Replies:
    1
    Views:
    353
    Mark McIntyre
    Dec 1, 2005
Loading...

Share This Page