wmemcpy() function in C/C++

The wmemcpy() function is specified in header file cwchar.h and copies a specified number of character from one string to the other. This function doesn’t check for any terminating null wide character in the first string called source it always copies exactly n characters to the second string called destination.

Syntax:

wchar_t* wmemcpy( wchar_t* destination, const wchar_t* source, size_t n )

Parameter : The function accepts three mandatory parameters which are described below:

  • destination: specifies a pointer where the characters are to be copied .
  • source: specifies a pointer where the data are present .
  • n: specifies the number of characters to be copied .

Return value: The function returns the destination string.

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustarte
// wmemcpy() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the destination size
    wchar_t destination[20];
  
    // initialize the source string
    wchar_t source[] = L"geeks are for geeks";
  
    // till number of characters
    int n = 13;
  
    // function to copy from source to
    // destination
    wmemcpy(destination, source, n);
  
    wcout << L"Initial string -> " << source << "\n";
  
    // print the copied string
    wcout << L"Final string -> ";
    for (int i = 0; i < n; i++)
        putwchar(destination[i]);
  
    return 0;
}

chevron_right


Output:

Initial string -> geeks are for geeks
Final string -> geeks are for

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustarte
// wmemcpy() function
// when 'n' is equal to the number of
// characters in the source string
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the destination size
    wchar_t destination[3];
  
    // initialize the source string
    wchar_t source[] = L"GFG";
  
    // till number of characters
    int n = 3;
  
    // function to copy from source to
    // destination
    wmemcpy(destination, source, n);
  
    wcout << L"Initial string -> " << source << "\n";
  
    // print the copied string
    wcout << L"Final string -> ";
    for (int i = 0; i < n; i++)
        putwchar(destination[i]);
  
    return 0;
}

chevron_right


Output:

Initial string -> GFG
Final string -> GFG


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.