memmove function-copy memory area in bytes

The memmove function is a function that copies the memory area in bytes. Include " string.h". The memcpy function is not guaranteed to work if the copy destination and copy source data areas overlap, but the memmove function is guaranteed. increase.

#include <string.h>

void * memmove (void * buf1, const void * buf2, size_t n);

The first argument is the address of the copy destination. General-purpose pointer type, so any pointer type is fine.

The second argument is the address of the copy source. General-purpose pointer type, so any pointer type is fine.

Specify the byte size in the third argument. It is size_t type. int32_t If the value is 0 or more in the range up to the type, it is safe first. Specifying 0 is a valid argument, in which case no copy will be done.

Delete the first character of the string

This is a sample to delete the first character of string with the memmove function. Since the areas overlap, use the memmove function. I am getting the length of the string with the strlen function.

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

int main (void) {
  // Variable name
  const char * var_name = "$foo";
  
  // Variable name length
  int32_t var_name_length = strlen (var_name);
  
  // Copy the variable name
  char * var_name_alphabet = calloc (var_name_length + 1, sizeof (char));
  memcpy (var_name_alphabet, var_name, var_name_length);
  
  // Make the variable name only the alphabet part
  memmove (var_name_alphabet, var_name_alphabet + 1, var_name_length --1);
  var_name_alphabet [var_name_length --1] ='\ 0';
  
  printf("%s\n", var_name_alphabet);
  
  free (var_name_alphabet);
}

This is the output result.

foo

Associated Information