Copy from char16_t* to char16_t*

1.2k Views Asked by At

I'm trying to copy a char16_t* that's passed into a function but haven't found any way to do that.

foo(char16_t* characters) {
    char16_t* copiedCharacters;
    //copy characters to copiedCharacters
}

I tried to use strncopy() but it only copies char*.

2

There are 2 best solutions below

8
Mateusz Drost On

Just adjust strcpy implementation

char16_t *mystrcpy(char16_t *dest, const char16_t *src)
{
  unsigned i;
  for (i=0; src[i] != '\0'; ++i)
    dest[i] = src[i];
  dest[i] = '\0';
  return dest;
}
2
Loreto On

There is a known construction (borrowed from strcpy):

char16_t* str16cpy(char16_t* destination, const char16_t* source)
{
    char16_t* temp = destination;
    while((*temp++ = *source++) != 0)
    ;
    return destination;
}