memcmp() semantics

Discussion in 'C Programming' started by Sidney Cadot, Nov 23, 2003.

  1. Sidney Cadot

    Sidney Cadot Guest

    Hi all,

    Just browsing through my newly-acquired C99 spec, I was reading on the
    memcmp() function (7.21.4.1). It has a rather peculiar wording:

    int memcmp(const void *s1, const void *s2, size_t n);

    1. The memcmp function compares the first n characters of the object
    pointed to by s1 to the first n characters of the object pointed to by s2.

    2. The memcmp function returns an integer greater than, equal to, or
    less than zero, accordingly as the object pointed to by s1 is greater
    than, equal to, or less than the object pointed to by s2.

    What do "greater than", "equal to", or "less than" mean in relation to
    the objects pointed to by s1 or s2?

    If I implement a C library that does signed-byte one-complement
    bit-reversed comparisons on the bytes, in reverse order (starting from
    (char *)s1[n-1] and (char *)s2[n-1], going down), would this be compliant?

    Best regards,

    Sidney
     
    Sidney Cadot, Nov 23, 2003
    #1
    1. Advertising

  2. Sidney Cadot

    James Hu Guest

    On 2003-11-23, Sidney Cadot <> wrote:
    >
    > Hi all,
    >
    > Just browsing through my newly-acquired C99 spec, I was reading on the
    > memcmp() function (7.21.4.1). It has a rather peculiar wording:
    > ...
    > What do "greater than", "equal to", or "less than" mean in relation to
    > the objects pointed to by s1 or s2?


    See 7.21.4 paragraph 1.

    "The sign of a nonzero value returned by the comparison functions
    memcmp, strcmp, and strncmp is determined by the sign of the
    difference between the values of the first pair of characters (both
    interpreted as unsigned char) that differ in the objects being
    compared."

    -- James
     
    James Hu, Nov 23, 2003
    #2
    1. Advertising

  3. Sidney Cadot

    Sidney Cadot Guest

    James Hu wrote:

    >>What do "greater than", "equal to", or "less than" mean in relation to
    >>the objects pointed to by s1 or s2?

    >
    >
    > See 7.21.4 paragraph 1.
    >
    > "The sign of a nonzero value returned by the comparison functions
    > memcmp, strcmp, and strncmp is determined by the sign of the
    > difference between the values of the first pair of characters (both
    > interpreted as unsigned char) that differ in the objects being
    > compared."


    This text is literally no more than 5 centimeters above the text I
    quoted - emberassing.

    Thanks, best regards,

    Sidney
     
    Sidney Cadot, Nov 23, 2003
    #3
  4. Sidney Cadot

    Dan Pop Guest

    In <bpq5hq$m1a$> Sidney Cadot <> writes:

    >James Hu wrote:
    >
    >>>What do "greater than", "equal to", or "less than" mean in relation to
    >>>the objects pointed to by s1 or s2?

    >>
    >>
    >> See 7.21.4 paragraph 1.
    >>
    >> "The sign of a nonzero value returned by the comparison functions
    >> memcmp, strcmp, and strncmp is determined by the sign of the
    >> difference between the values of the first pair of characters (both
    >> interpreted as unsigned char) that differ in the objects being
    >> compared."

    >
    >This text is literally no more than 5 centimeters above the text I
    >quoted - emberassing.


    As a general rule, don't read the individual specification of a function
    *before* the paragraph(s) prefacing the respective section and subsection.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Nov 24, 2003
    #4
  5. Sidney Cadot

    Sidney Cadot Guest

    Dan Pop wrote:

    >>This text is literally no more than 5 centimeters above the text I
    >>quoted - emberassing.

    >
    > As a general rule, don't read the individual specification of a function
    > *before* the paragraph(s) prefacing the respective section and subsection.


    ....Surely the single best piece of advice since the time I was clearing
    my shoes from poop, and somebody pointed out that I shouldn't step on a
    turd.

    As then, I will try in the future.

    Best regards,

    Sidney
     
    Sidney Cadot, Nov 25, 2003
    #5
  6. Sidney Cadot

    nobody Guest

    "Sidney Cadot" <> wrote in message
    news:bpq5hq$m1a$...
    > James Hu wrote:
    >
    > >>What do "greater than", "equal to", or "less than" mean in relation to
    > >>the objects pointed to by s1 or s2?

    > >
    > >
    > > See 7.21.4 paragraph 1.
    > >
    > > "The sign of a nonzero value returned by the comparison functions
    > > memcmp, strcmp, and strncmp is determined by the sign of the
    > > difference between the values of the first pair of characters (both
    > > interpreted as unsigned char) that differ in the objects being
    > > compared."

    >
    > This text is literally no more than 5 centimeters above the text I
    > quoted - emberassing.
    >

    Now, what about 7.1.1p1 "A pointer to a string is a pointer to
    its initial (lowest addressed) character." Where is the *real*
    beginning (of a string)? How do we know which character ('first'
    or 'last') is at lowest address (James' quote about *first* pair)?
    I have a feeling I'm wrong, just need some standard* ptr as to
    where.
     
    nobody, Nov 25, 2003
    #6
  7. On Tue, 25 Nov 2003, nobody wrote:
    >
    > Now, what about 7.1.1p1 "A pointer to a string is a pointer to
    > its initial (lowest addressed) character." Where is the *real*
    > beginning (of a string)? How do we know which character ('first'
    > or 'last') is at lowest address (James' quote about *first* pair)?
    > I have a feeling I'm wrong, just need some standard* ptr as to
    > where.


    To determine whether pointer P contains a lower value (points
    to a lower address) than pointer Q, you can use the < operator:

    if (P < Q) { ... }

    In general, it should be obvious that (&s[0] < &s[k]) for
    all positive values of k. :)

    (Note, of course, that the semantics of < are only defined
    when P and Q point to elements of the same array, or one past
    the last element of that array; where single objects count as
    one-element "arrays" for purposes of simplification. You
    can't portably compare any old pair of pointers.)

    -Arthur
     
    Arthur J. O'Dwyer, Nov 25, 2003
    #7
    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. memcmp for <

    , Mar 23, 2005, in forum: C++
    Replies:
    16
    Views:
    1,023
    Jack Klein
    Mar 24, 2005
  2. Joona I Palaste

    Re: memcmp versus strstr; reaction to chr(0)

    Joona I Palaste, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    435
    Joona I Palaste
    Jul 24, 2003
  3. Thomas Matthews

    Re: memcmp versus strstr; reaction to chr(0)

    Thomas Matthews, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    535
    Thomas Matthews
    Jul 24, 2003
  4. Burne C
    Replies:
    3
    Views:
    1,352
    Peter Ammon
    Jul 25, 2003
  5. Dan Pop
    Replies:
    0
    Views:
    387
    Dan Pop
    Jul 24, 2003
Loading...

Share This Page