[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppMemcmp
Don't use it on non-plain-old-data types.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppMemcmp
(C++) memcmp
C++ Standard [1] function for comparing two buffers (of type char*).int main () { const int bufferSize = 256; char str1[bufferSize]; char str2[bufferSize]; std::cout << "Enter sentence #1: \n"; gets(str1); std::cout << "Enter sentence #2: \n"; gets(str2); const int length1 = std::strlen(str1); const int length2 = std::strlen(str2); const int comparison = std::memcmp (str1, str2, std::max(length1,length2)); if (comparison>0) { std::cout << str1 << " is greater than " << str2 << std::endl; } else if (comparison<0) { std::cout << str1 << " is less than " << str2 << std::endl; } else { std::cout << str1 << " is equal to " << str2 << std::endl; } }
- include <iostream>
- include <cstdio>
- include <cstring>
Don't use it on non-plain-old-data types.
'memcmp' links
Reference
- 1) C++ International Standard. ISO/IEC 14882. Second edition. Table 99
- 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6, chapter 96: don't memcpy and memcmp non-POD's
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
