Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memcmp() semantics

Reply
Thread Tools

memcmp() semantics

 
 
Sidney Cadot
Guest
Posts: n/a
 
      11-23-2003

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

 
Reply With Quote
 
 
 
 
James Hu
Guest
Posts: n/a
 
      11-23-2003
On 2003-11-23, Sidney Cadot <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Sidney Cadot
Guest
Posts: n/a
 
      11-23-2003
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

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-24-2003
In <bpq5hq$m1a$(E-Mail Removed)> Sidney Cadot <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Sidney Cadot
Guest
Posts: n/a
 
      11-25-2003
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

 
Reply With Quote
 
nobody
Guest
Posts: n/a
 
      11-25-2003
"Sidney Cadot" <(E-Mail Removed)> wrote in message
news:bpq5hq$m1a$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      11-25-2003

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
 
Reply With Quote
 
 
 
Reply

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
it's all about semantics =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?= ASP .Net 2 07-14-2004 08:19 AM
Semantics extractor Luigi Donatello Asero HTML 5 02-14-2004 07:10 PM
Equivalency Semantics, Operator Overloading Grant Austin C++ 3 12-09-2003 05:03 PM
std::vector<>::clear semantics Stefan Höhne C++ 10 10-17-2003 09:50 AM
destructor / semantics of delete this Alexander Stippler C++ 6 08-20-2003 03:27 PM



Advertisments