Connecting Tech Pros Worldwide Forums | Help | Site Map

memcmp() semantics

Sidney Cadot
Guest
 
Posts: n/a
#1: Nov 13 '05

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


James Hu
Guest
 
Posts: n/a
#2: Nov 13 '05

re: memcmp() semantics


On 2003-11-23, Sidney Cadot <sidney@jigsaw.nl> wrote:[color=blue]
>
> 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?[/color]

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
Sidney Cadot
Guest
 
Posts: n/a
#3: Nov 13 '05

re: memcmp() semantics


James Hu wrote:
[color=blue][color=green]
>>What do "greater than", "equal to", or "less than" mean in relation to
>>the objects pointed to by s1 or s2?[/color]
>
>
> 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."[/color]

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

Thanks, best regards,

Sidney

Dan Pop
Guest
 
Posts: n/a
#4: Nov 13 '05

re: memcmp() semantics


In <bpq5hq$m1a$1@news.tudelft.nl> Sidney Cadot <sidney@jigsaw.nl> writes:
[color=blue]
>James Hu wrote:
>[color=green][color=darkred]
>>>What do "greater than", "equal to", or "less than" mean in relation to
>>>the objects pointed to by s1 or s2?[/color]
>>
>>
>> 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."[/color]
>
>This text is literally no more than 5 centimeters above the text I
>quoted - emberassing.[/color]

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@ifh.de
Sidney Cadot
Guest
 
Posts: n/a
#5: Nov 13 '05

re: memcmp() semantics


Dan Pop wrote:
[color=blue][color=green]
>>This text is literally no more than 5 centimeters above the text I
>>quoted - emberassing.[/color]
>
> As a general rule, don't read the individual specification of a function
> *before* the paragraph(s) prefacing the respective section and subsection.[/color]

....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

nobody
Guest
 
Posts: n/a
#6: Nov 13 '05

re: memcmp() semantics


"Sidney Cadot" <sidney@jigsaw.nl> wrote in message
news:bpq5hq$m1a$1@news.tudelft.nl...[color=blue]
> James Hu wrote:
>[color=green][color=darkred]
> >>What do "greater than", "equal to", or "less than" mean in relation to
> >>the objects pointed to by s1 or s2?[/color]
> >
> >
> > 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."[/color]
>
> This text is literally no more than 5 centimeters above the text I
> quoted - emberassing.
>[/color]
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.


Arthur J. O'Dwyer
Guest
 
Posts: n/a
#7: Nov 13 '05

re: memcmp() semantics



On Tue, 25 Nov 2003, nobody wrote:[color=blue]
>
> 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.[/color]

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
Closed Thread