By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

memcmp() semantics

P: n/a

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

Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 2003-11-23, Sidney Cadot <si****@jigsaw.nl> 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
Nov 13 '05 #2

P: n/a
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

Nov 13 '05 #3

P: n/a
In <bp**********@news.tudelft.nl> Sidney Cadot <si****@jigsaw.nl> 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: Da*****@ifh.de
Nov 13 '05 #4

P: n/a
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

Nov 13 '05 #5

P: n/a
"Sidney Cadot" <si****@jigsaw.nl> wrote in message
news:bp**********@news.tudelft.nl...
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.
Nov 13 '05 #6

P: n/a

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
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.