Chapter Contents

Previous

Next
memcmp

memcmp



Compare Two Blocks of Memory

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

int memcmp(const void *ptr1, const void *ptr2, size_t n);


DESCRIPTION

memcmp compares two blocks of memory specified by ptr1 and ptr2 . The number of bytes to be compared is n . The null character is treated like any other character and participates in the comparison, which is performed using the standard EBCDIC collating sequence.


RETURN VALUE

memcmp returns 0 if the two blocks are equal, an integer less than 0 if the first block is less than the second, or an integer greater than 0 if the first block is greater than the second.


IMPLEMENTATION

The compiler generates inline code for memcmp unless memcmp is undefined (by an #undef statement) to prevent this. The inline code may still call a library routine in special cases (for example, if the length is a variable whose value is larger than 16 megabytes). For more information on optimizing your use of memcmp , see Optimizing Your Use of memcmp, memcpy, and memset.

Usually, the code generated by memcmp uses the CLCL instruction to perform the comparison. If more than 16 megabytes of data are to be compared, the library routine is called, which processes 16M-1 bytes at a time.


EXAMPLE

#include <string.h>
#include <stdio.h>

main()
{
   struct large {
      int month;
      int day;
      int year;
   };

   struct large day1, day2, *first, *second;

   day1.month = 7;
   day1.day = 29;
   day1.year = 1993;
   day2.month = 7;
   day2.day = 30;
   day2.year = 1993;
   first = &day1;
   second = &day2;

      /* Compare one structure to another. Note: if structures */
      /*  contain padding between elements, the results may be */
      /*  misleading.                                          */
   if (memcmp((char *)first,(char *)second, sizeof(struct large)))
       puts("structures not equal");
   else
       puts("structures are equal");
}


RELATED FUNCTIONS

memcmpp , strcmp , strncmp


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.