C – Buffer manipulation function

      Prev                                                                                              Next

  • Buffer manipulation functions in C work on the address of the memory block rather than the values inside the address.
  • Example programs for memset(), memcpy(), memmove(), memcmp(), memicmp() and memchr() functions are given below.

……

S.no
Function
Description
1 memset() It is used to initialize a specified number of bytes to null or any other value in the buffer
2 memcpy() It is used to copy a specified number of bytes from one memory to another
3 memmove() It is used to copy a specified number of bytes from one memory to another or to overlap on same memory. 
Difference between memmove and memcpy is, overlap can happen on memmove whereas memcpy should be done in non-destructive way
4 memcmp() It is used to compare specified number of characters from two buffers
5 memicmp() It is used to compare specified number of characters from two buffers  regardless of the case of the characters
6 memchr() It is used to locate the first occurrence of the character in the specified string

 Example program for memset() function in C:

    • memset( ) function is used to initialize specified number of bytes to null or to any other value in the buffer.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
   int i;
   /* allocate memory for array of 5 elements */
   char *a = (char *) malloc(5*sizeof(char));
   printf("Values before memsetn");
   for (i = 0; i < 5; ++i)
       printf("  a[%d] = %d ,", i,    a[i]);

   /* All elements are set to 3. It can be set to any value */
   memset(a, 3, 5*sizeof(char));
      printf("nValues after memsetn");
   for (i = 0; i < 5; ++i)
       printf("  a[%d] = %d ,", i,    a[i]);
   // remove x from memory
   free(a);
   return 0;
}

 Output:

Values before memset
a[0] = 0 , a[1] = 0 , a[2] = 0 , a[3] = 0 , a[4] = 0
Values after memset
a[0] = 3 , a[1] = 3 , a[2] = 3 , a[3] = 3 , a[4] = 3

Example program for memcpy() function in C:

    • memcpy( ) function is used to copy a specified number of bytes from one memory to another.
#include <stdio.h>
#include <string.h>
int main()
{
   // define two identical arrays
   char str1[10] = "fresh";
   char str2[10] ;
   if (memcpy(str2,str1, strlen(str1)))
   {
      printf("Elements in str1 are copied to str2 .n");
      printf("str1 = %sn str2 = %s n", str1, str2);
   }
   else
     printf("Error while coping str1 into str2.n");
   return 0;
}

Output:

Elements in str1 are copied to str2 .
str1 = fresh
str2 = fresh

Example program for memmove() function in C:

    • memmove( ) function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.
#include <stdio.h>
#include <string.h>
int main()
{
  // define two identical arrays
  char str1[10] = "fresh";

    printf("str1 before memmoven");
    printf("str1 = %sn ", str1);

  if (memmove(str1+2,str1, strlen(str1)))
  {
    printf("Elements in str1 are moved/overlapped on str1.n");
    printf("str1 = %s n", str1);
  }
  else
    printf("Error while coping str1 into str2.n");
  return 0;
}

 Output:

str1 before memmove
str1 = fresh
Elements in str1 are moved/overlapped on str1 .
str1 = frfresh

Example program for memcmp() function in C:

    • memcmp( ) function is used to compare specified number of characters from two buffers.
#include <stdio.h>
#include <string.h>
int main()
{
  // define two identical arrays
  char str1[10] = "fresh";
  char str2[10] = "refresh";
  if (!memcmp(str1,str2, 5*sizeof(char)))
    printf("Elements in str1 and str2 are same.n");
  else
    printf("Elements in str1 and str2 are not same.n");
  return 0;
}

 Output:

Elements in str1 and str2 are not same.

Example program for memicmp() function in C:

    • memicmp( ) function is used to compare specified number of characters from two buffers  regardless of the case of the characters.
    • If we use memcmp() function instead of memicmp, the output of the below program will be “Elements in str1 and str2 are not same”.
#include <stdio.h>
#include <string.h>
int main()
{
  // define two identical arrays
  char str1[10] = "fresh";
  char str2[10] = "FRESH";
  if (!memicmp(str1,str2, 5*sizeof(char)))
    printf("Elements in str1 and str2 are same.n");
  else
    printf("Elements in str1 and str2 are not same.n");
  return 0;
}

 Output:

Elements in str1 and str2 are same.

Example program for memchr() function in C:

    • memchr( ) function is used to locate the first occurrence of the character in the specified string.
#include <stdio.h>
#include <string.h>

int main ()
{
  char *ptr;
  char string[] = "cprogramming";
  ptr = (char *) memchr (string, 'h', strlen(string));
  if (ptr != NULL)
    printf ("character 'h' is found at " 
            "position %d.n", ptr-string+1);
  else
    printf ("character 'h' is not found.n");
  return 0;
}

Output:

character ‘h’ is found at position 5.

      Prev                                                                                              Next

.