Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/memcmp"

From cppreference.com
< cpp‎ | string‎ | byte
m (Update links.)
(Notes: note endianess)
 
(11 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/title| memcmp}}
+
{{cpp/title|memcmp}}
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
{{ddcl | header=cstring |
+
{{ddcl|header=cstring|
 
int memcmp( const void* lhs, const void* rhs, std::size_t count );
 
int memcmp( const void* lhs, const void* rhs, std::size_t count );
 
}}
 
}}
  
Reinterprets the objects pointed to by {{tt|lhs}} and {{tt|rhs}} as arrays of {{c|unsigned char}} and compares the first {{tt|count}} characters of these arrays. The comparison is done lexicographically.
+
Reinterprets the objects pointed to by {{c|lhs}} and {{c|rhs}} as arrays of {{c/core|unsigned char}} and compares the first {{c|count}} bytes of these arrays. The comparison is done lexicographically.
 +
 
 +
The sign of the result is the sign of the difference between the values of the first pair of bytes (both interpreted as {{c/core|unsigned char}}) that differ in the objects being compared.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | lhs, rhs | pointers to the memory buffers to compare}}
+
{{par|lhs, rhs|pointers to the memory buffers to compare}}
{{par | count | number of bytes to examine}}
+
{{par|count|number of bytes to examine}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Negative value if the first differing byte (reinterpreted as {{c|unsigned char}}) in {{tt|lhs}} is less than the corresponding byte in {{tt|rhs}}.
+
Negative value if the first differing byte (reinterpreted as {{c/core|unsigned char}}) in {{c|lhs}} is less than the corresponding byte in {{c|rhs}}.
  
{{c|0}} if all {{tt|count}} bytes of {{tt|lhs}} and {{tt|rhs}} are equal.
+
{{c|0}} if all {{c|count}} bytes of {{c|lhs}} and {{c|rhs}} are equal.
  
Positive value if the first differing byte in {{tt|lhs}} is greater than the corresponding byte in {{tt|rhs}}.
+
Positive value if the first differing byte in {{c|lhs}} is greater than the corresponding byte in {{c|rhs}}.
  
 
===Notes===
 
===Notes===
This function reads object representations, not the object values, and is typically meaningful for trivially-copyable objects only. For example, {{tt|memcmp()}} between two objects of type {{lc|std::string}} or {{lc|std::vector}} will not compare their contents.
+
This function reads [[cpp/language/object#Object representation and value representation|object representations]], not the object values, and is typically meaningful for only trivially-copyable objects that have no padding. For example, {{tt|memcmp()}} between two objects of type {{lc|std::string}} or {{lc|std::vector}} will not compare their contents, {{tt|memcmp()}} between two objects of type {{c|struct { char c; int n; }<!---->}} will compare the padding bytes whose values may differ when the values of {{c|c}} and {{c|n}} are the same, and even if there were no padding bytes, the {{tt|int}} would be compared without taking into account endianness.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
 
#include <cstring>
 
#include <cstring>
 +
#include <iostream>
 +
 +
void demo(const char* lhs, const char* rhs, std::size_t sz)
 +
{
 +
    std::cout << std::string(lhs, sz);
 +
    const int rc = std::memcmp(lhs, rhs, sz);
 +
    if (rc < 0)
 +
        std::cout << " precedes ";
 +
    else if (rc > 0)
 +
        std::cout << " follows ";
 +
    else
 +
        std::cout << " compares equal to ";
 +
    std::cout << std::string(rhs, sz) << " in lexicographical order\n";
 +
}
  
 
int main()
 
int main()
 
{
 
{
 
     char a1[] = {'a', 'b', 'c'};
 
     char a1[] = {'a', 'b', 'c'};
     char a2[] = {'a', 'b', 'd'};
+
     char a2[sizeof a1] = {'a', 'b', 'd'};
 
+
   
     std::cout << "'abc' vs 'abd': " << std::memcmp(a1, a2, sizeof a1) << '\n'
+
     demo(a1, a2, sizeof a1);
              << "'abd' vs 'abc': " << std::memcmp(a2, a1, sizeof a1) << '\n'
+
    demo(a2, a1, sizeof a1);
              << "'abc' vs 'abc': " << std::memcmp(a1, a1, sizeof a1) << '\n';
+
    demo(a1, a1, sizeof a1);
 
}
 
}
| output=
+
|output=
'abc' vs 'abd': -1
+
abc precedes abd in lexicographical order
'abd' vs 'abc': 1
+
abd follows abc in lexicographical order
'abc' vs 'abc': 0
+
abc compares equal to abc in lexicographical order
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/byte/dsc strcmp}}
+
{{dsc inc|cpp/string/byte/dsc strcmp}}
{{dsc inc | cpp/string/byte/dsc strncmp}}
+
{{dsc inc|cpp/string/byte/dsc strncmp}}
{{dsc see c | c/string/byte/memcmp}}
+
{{dsc see c|c/string/byte/memcmp}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/byte/memcmp]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/byte/memcmp]]
+
[[fr:cpp/string/byte/memcmp]]
+
[[it:cpp/string/byte/memcmp]]
+
[[ja:cpp/string/byte/memcmp]]
+
[[pt:cpp/string/byte/memcmp]]
+
[[ru:cpp/string/byte/memcmp]]
+
[[zh:cpp/string/byte/memcmp]]
+

Latest revision as of 03:19, 10 July 2023

Defined in header <cstring>
int memcmp( const void* lhs, const void* rhs, std::size_t count );

Reinterprets the objects pointed to by lhs and rhs as arrays of unsigned char and compares the first count bytes of these arrays. The comparison is done lexicographically.

The sign of the result is the sign of the difference between the values of the first pair of bytes (both interpreted as unsigned char) that differ in the objects being compared.

Contents

[edit] Parameters

lhs, rhs - pointers to the memory buffers to compare
count - number of bytes to examine

[edit] Return value

Negative value if the first differing byte (reinterpreted as unsigned char) in lhs is less than the corresponding byte in rhs.

0 if all count bytes of lhs and rhs are equal.

Positive value if the first differing byte in lhs is greater than the corresponding byte in rhs.

[edit] Notes

This function reads object representations, not the object values, and is typically meaningful for only trivially-copyable objects that have no padding. For example, memcmp() between two objects of type std::string or std::vector will not compare their contents, memcmp() between two objects of type struct { char c; int n; } will compare the padding bytes whose values may differ when the values of c and n are the same, and even if there were no padding bytes, the int would be compared without taking into account endianness.

[edit] Example

#include <cstring>
#include <iostream>
 
void demo(const char* lhs, const char* rhs, std::size_t sz)
{
    std::cout << std::string(lhs, sz);
    const int rc = std::memcmp(lhs, rhs, sz);
    if (rc < 0)
        std::cout << " precedes ";
    else if (rc > 0)
        std::cout << " follows ";
    else
        std::cout << " compares equal to ";
    std::cout << std::string(rhs, sz) << " in lexicographical order\n";
}
 
int main()
{
    char a1[] = {'a', 'b', 'c'};
    char a2[sizeof a1] = {'a', 'b', 'd'};
 
    demo(a1, a2, sizeof a1);
    demo(a2, a1, sizeof a1);
    demo(a1, a1, sizeof a1);
}

Output:

abc precedes abd in lexicographical order
abd follows abc in lexicographical order
abc compares equal to abc in lexicographical order

[edit] See also

compares two strings
(function) [edit]
compares a certain number of characters from two strings
(function) [edit]
C documentation for memcmp