473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

memcmp() semantics


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
6 3913
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
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
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
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
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: rajkumar | last post by:
I have a struct like struct MyStruct { int a; int b; int c: bool d; bool e; }
4
by: blueblueblue2005 | last post by:
hi, I am reading the description of memcmp and strcmp, it seems there is no big difference between these two functions. except that memcmp takes void pointer parameters. so why string.h has these...
2
by: Mads Jacobsen | last post by:
Hi, Does anyone know the c#-function of c++'s memcmp()? Regards, Mads
7
by: Alex Stark | last post by:
Hi, I'm new to c# an missing functions like memcmp and memcpy from C. Is there something similar ? cu Alex
4
by: Preets | last post by:
Hi, Can anybody please tell me the difference between memcmp() and strncmp() functions ?
1
by: sahilmakkar | last post by:
can anybody tell me what is difference between memcmp and strcmp which one is better efficient and when to use memcmp or strcmp
4
by: system55 | last post by:
which of the 2 commands are applicable in comparing an array of unsigned chars? if(strcmp(aAbsCylNumHigh, bAbsCylNumHigh)<=0 && strcmp(aAbsCylNumLow,bAbsCylNumLow)<=0 && strcmp(aSecNum,.bSecNum)<0...
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
39
by: Martin | last post by:
Please consider the following code fragment. Assume UINT32 is a typedef suitable for defining variables of 32 bits, and that ui32 is initialised. UINT32 ui32; /* ... */ /* assume ui32 now is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.