Skip to content

Instantly share code, notes, and snippets.

@yclim95
Created May 24, 2022 07:44
Show Gist options
  • Save yclim95/05293a9b0b54dbf0b056853f560e61c9 to your computer and use it in GitHub Desktop.
Save yclim95/05293a9b0b54dbf0b056853f560e61c9 to your computer and use it in GitHub Desktop.
memmove() in c

memmove() in c - Explanation

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100] = "Learningisfun";
    char *first, *second;
    first = str;
    second = str;
    printf("Original string :%s\n ", str);
      
    // when overlap happens then it just ignore it
    memcpy(first + 8, first, 10);
    printf("memcpy overlap : %s\n ", str);
    printf("memmove overlap - first : %s\n ", first);
    
    // when overlap it start from first position
    memmove(second + 8, first, 10);
    printf("memmove overlap : %s\n ", str);
    printf("memmove overlap[9] : %s\n ", second + 9);
    printf("memmove overlap - first : %s\n ", first);
    printf("memmove overlap - second : %s\n ", second);
    printf("memmove sizeof : %ld\n ", sizeof(first));
    return 0;
}
Original string :Learningisfun
 memcpy overlap : LearningLearningis
 memmove overlap - first : LearningLearningis
 memmove overlap : LearningLearningLe
 memmove overlap[9] : earningLe
 memmove overlap - first : LearningLearningLe
 memmove overlap - second : LearningLearningLe
 memmove sizeof : 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment