memcpy c++ code example

Example 1: memcpy c++ usage

#include<cstring> // or string.h
int main(){
	char a[4],b[]={"hello"};
  	memcpy(a,b,strlen(b)*sizeof(char));//destination,source,size*sizeof(type)
  	
}

Example 2: c memcpy

int dst[ARRAY_LENGTH];
memcpy( dst, src, sizeof(dst) ); // Good, sizeof(dst) returns sizeof(int) * ARRAY_LENGTH

Example 3: memcpy library cpp

#include <cstring>

Example 4: c++ memcmp

/* memcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=memcmp ( buffer1, buffer2, sizeof(buffer1) );

  if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}

Tags:

C Example