memcmp() semantics

S

Sidney Cadot

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
 
J

James Hu

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
 
S

Sidney Cadot

James said:
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
 
D

Dan Pop

In said:
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
 
S

Sidney Cadot

Dan said:
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
 
N

nobody

Sidney Cadot said:
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.
 
A

Arthur J. O'Dwyer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top