Why does wmemcpy exist when memcpy should suffice?

前端 未结 2 1471
别跟我提以往
别跟我提以往 2021-01-18 19:13

wmemcpy appears to perform the same operations as memcpy but accepts wchar_t* instead of void*. How is its existence just

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 19:17

    In addition to davmac's answer about API symmetry and having the size of an array not always granted, it should be emphasized that the third argument of wmemcpy refers to number of elements to copy (rather than bytes).

    If you work with wchar_t objects and handle them with other functions from , it may facilitate matters. wcslen for instance returns the C wide string length in terms of wchar_t elements, and wcschr and wcsrchr return wchar_t *, thus using them to do some pointer arithmetic also keeps you in the "realm" of number-of-elements.

    P.S. If the size of the minimal wchar_t array is given as implied in your example, using wmemcpy may result in more elegant code than that sizeof(wchar_array) you used:

    #define SIZE 40
    wchar_t wchar_array[SIZE];
    // ...
    wmemcpy(dest, wchar_array, SIZE);
    

提交回复
热议问题