Tuesday, 17 January 2012

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"

Sunday, 15 January 2012

Realloc function

Synopsis:
#include <stdio.h>
void *realloc(void *mem_address, int newsize); 
Description:
The size of the memory block pointed to by the mem_address parameter is changed to the newsize bytes, expanding or reducing the amount of memory available in the block.
In case that mem_address is NULL, the function behaves exactly as malloc, assigning a new block of newsize bytes and returning a pointer to the beginning of it.
In case that the newsize is 0, the memory previously allocated in mem_address is deallocated as if a call to free was made, and a NULL pointer is returned.
Return Value
A pointer to the reallocated memory block, which may be either the same as the mem_address argument or a new location.
Example
#include <stdio.h>
int main ()
{
  int input,n;
  int count=0;
  int * numbers = NULL;
  do {
     printf ("Enter an integer value (0 to end): ");
     scanf ("%d", &input);
     count++;
     numbers = (int*) realloc (numbers, count * sizeof(int));
     if (numbers==NULL)
       { puts ("Error (re)allocating memory"); exit (1); }
     numbers[count-1]=input;
  } while (input!=0);
  printf ("Numbers entered: ");
  for (n=0;n<count;n++) printf ("%d ",numbers[n]);
  free (numbers);
  return 0;
}
It will proiduce following result:
Enter an integer value (0 to end): 2
Enter an integer value (0 to end): 3
Enter an integer value (0 to end): 4
Enter an integer value (0 to end): 5
Enter an integer value (0 to end): 0
Numbers entered: 2 3 4 5 0

malloc function

Synopsis:
#include <stdio.h>
void * malloc ( int size );
Description:
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
Return Value
On success, a pointer to the memory block allocated by the function. If fails a NULL value is returned.
Example
#include <stdio.h>
int main ()
{
  int i,n;
  int * pData;
  printf ("Amount of numbers to be entered: ");
  scanf ("%d",&i);
  pData = (int*) malloc (i*sizeof(int));
  if (pData==NULL) exit (1);
  for (n=0;n<i;n++)
  {
    printf ("Enter number #%d: ",n);
    scanf ("%d",&pData[n]);
  }
  printf ("You have entered: ");
  for (n=0;n<i;n++) printf ("%d ",pData[n]);
  free (pData);
  return 0;
}
It will proiduce following result:
Amount of numbers to be entered: 10
Enter number #0: 2
Enter number #1: 3
Enter number #2: 3
Enter number #3: 4
Enter number #4: 5
Enter number #5: 6
Enter number #6: 7
Enter number #7: 8
Enter number #8: 3
Enter number #9: 9
You have entered: 2 3 3 4 5 6 7 8 3 9
host gator coupon