How to use memcpy() c?

51 Views Asked by At

I want to put "12345" instead of "defgh"

After the execution of the code written below, this is what i obtain : original : abc12345

but this is the output that i am looking for : abc12345ijklmnop

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

int main() {
  char original[20] = "abcdefghijklmnop";
  char src[6] = "12345";
  memcpy(original+3, src, sizeof(char) * 6);
  printf("original : %s\n", original);
}

Thank You

1

There are 1 best solutions below

0
dbush On

You're copying over the terminating null byte in src. You want to leave that out.

memcpy(original+3, src, sizeof(src) - 1);