Showing posts with label Buffer Manipulation. Show all posts
Showing posts with label Buffer Manipulation. Show all posts

Tuesday, 17 January 2012

C - memmove

Synopsis:
#include <stdio.h>
void* memmove(void* s, const void* ct, int n); 
Description:
Copies n characters from ct to s and returns s. s will not be corrupted if objects overlap.
Return Value
The memmove function returns s after moving n characters.
Example
#include <stdio.h>
int main() {
  static char buf [] = "This is line 1 \n"
                       "This is line 2 \n"
                       "This is line 3 \n";
  printf ("buf before = %s\n", buf);
  memmove (&buf [0], &buf [16], 32);
  printf ("buf after = %s\n", buf);
  return 0;
}
It will proiduce following result:
buf before = This is line 1
This is line 2
This is line 3
buf after = This is line 2
This is line 3
This is line 3

C - memset

Synopsis:
#include <stdio.h>
void* memset(void* s, int c, int n);
Description:
The memset function sets the first n bytes in s to c. Generally this function is used to set a memory location to null chracters '\0'.
Return Value
The memset function returns s with the set value to c
Example
#include <stdio.h>
int main() {
  char string[20];
  strcpy(string, "Hello");
  printf( "Before using memset |%s|\n", string );
  memset( string, '\0', sizeof(string) );
  printf( "After using memset |%s|\n", string );
  return 0;
}
It will proiduce following result:
Before using memset |Hello|
After using memset ||

C - memchr

Synopsis:
#include <stdio.h>
void* memchr(const void* cs, int c, int n);
Description:
The memchr function scans cs for the character c in the first n bytes of the buffer.
Return Value
The memchr function returns a pointer to the character c in cs or a null pointer if the character was not found.
Example
#include <stdio.h>
int main()
{
  char src [100] = "Search this string from the start";
  char *c;
  c = (char*)memchr (src, 'g', sizeof (src));
  if (c == NULL)
    printf ("'g' was not found in the src\n");
  else
    printf ("found 'g' in the src\n");
  return 0;
}
It will proiduce following result:
found 'g' in the src

C - memcmp function

Synopsis:
#include <stdio.h>
int memcmp(const void* cs, const void* ct, int n);
Description:
The memcmp function compares the first n bytes from cs and ct and returns a value indicating their relationship as follows:
Return Value
if Return value if < 0 then it indicates cs less than ct
if Return value if > 0 then it indicates ct is less than cs
if Return value if = 0 then it indicates cs is equal to ct
Example
#include <stdio.h>
int main()
{
  char hexchars [] = "0123456789ABCDEF";
  char hexchars2 [] = "0123456789abcdef";
  char i;
  i = memcmp (hexchars, hexchars2, 16);
  if (i < 0)
    printf ("hexchars < hexchars2\n");
  else if (i > 0)
    printf ("hexchars >  hexchars2\n");
  else
    printf ("hexchars == hexchars2\n");
  return 0;
}
It will proiduce following result:
hexchars < hexchars2

C - memcpy function

Synopsis:
#include <stdio.h>
void* memcpy(void* s, const void* ct, int n);
Description:
The memcpy function copies n bytes from ct to s. If these memory buffers overlap, the memcpy function cannot guarantee that bytes in ct are copied to s before being overwritten. If these buffers do overlap, use the memmove function.
Return Value
The memcpy function returns dest.
Example
#include <stdio.h>
int main()
{
  char src [100] = "Copy this string to dst1";
  char dst [100];
  char *p;
  p = memcpy (dst, src, sizeof (dst));
  printf ("dst = \"%s\"\n", p);
}
It will proiduce following result:
dst = "Copy this string to dst1"
host gator coupon