📜  C / C++中的wmemcpy()函数

📅  最后修改于: 2021-05-26 00:51:52             🧑  作者: Mango

wmemcpy()函数在头文件cwchar.h中指定,并将指定数量的字符从一个字符串复制到另一个字符串。这个函数不会在第一个叫做source的字符串检查任何终止的空宽字符,它总是将精确的n个字符复制到第二个叫做destination的字符串。

句法:

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

参数:该函数接受三个强制性参数,如下所述:

  • destination:指定要在其中复制字符的指针。
  • source:指定数据存在的指针。
  • n:指定要复制的字符数。

返回值:该函数返回目标字符串。

下面的程序说明了上述函数:

程序1:

C++
// C++ program to illustrate
// wmemcpy() function
 
#include 
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;
}


C++
// C++ program to illustrate
// wmemcpy() function
// when 'n' is equal to the number of
// characters in the source string
#include 
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;
}


输出:
Initial string -> geeks are for geeks
Final string -> geeks are for

程序2:

C++

// C++ program to illustrate
// wmemcpy() function
// when 'n' is equal to the number of
// characters in the source string
#include 
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;
}
输出:
Initial string -> GFG
Final string -> GFG
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。