Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/wide/wmemcpy"

From cppreference.com
< cpp‎ | string‎ | wide
m ({{c}})
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{c/title|wmemcpy}}
+
{{cpp/title|wmemcpy}}
{{c/string/wide/sidebar}}
+
{{cpp/string/wide/navbar}}
{{ddcl | header=wchar.h |
+
{{ddcl|header=cwchar|
wchar_t* wmemcpy( wchar_t* dest, const wchar_t* src, size_t count );
+
wchar_t* wmemcpy( wchar_t* dest, const wchar_t* src, std::size_t count );
 
}}
 
}}
  
Copies {{tt|count}} wide characters from the object pointed to by {{tt|src}} to the object pointed to by {{tt|dest}}. If the objects overlap, the behavior is undefined.
+
Copies exactly {{c|count}} successive wide characters from the wide character array pointed to by {{c|src}} to the wide character array pointed to by {{c|dest}}. If the objects overlap, the behavior is undefined. If {{c|count}} is zero, the function does nothing.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | dest | pointer to the memory location to copy to}}
+
{{par|dest|pointer to the wide character array to copy to}}
{{param list item | src | pointer to the memory location to copy from}}
+
{{par|src|pointer to the wide character array to copy from}}
{{param list item | count | number of bytes to copy}}
+
{{par|count|number of wide characters to copy}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 +
{{c|dest}}
  
{{tt|dest}}
+
===Notes===
 +
This function's analog for byte strings is {{lc|std::strncpy}}, not {{lc|std::strcpy}}.
 +
 
 +
This function is not locale-sensitive and pays no attention to the values of the {{c|wchar_t}} objects it copies: nulls as well as invalid characters are copied too.
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
|
+
|code=
| code=
+
#include <clocale>
| output=
+
#include <cwchar>
 +
#include <iostream>
 +
#include <iterator>
 +
#include <locale>
 +
 
 +
int main(void)
 +
{
 +
    const wchar_t from1[] = L"नमस्ते";
 +
    const wchar_t from2[] = L"Բարև";
 +
    const std::size_t sz1 = std::size(from1);
 +
    const std::size_t sz2 = std::size(from2);
 +
    wchar_t to[sz1 + sz2];
 +
 
 +
    std::wmemcpy(to, from1, sz1); // copy from1, along with its null terminator
 +
    std::wmemcpy(to + sz1, from2, sz2); // append from2, along with its null terminator
 +
 
 +
    std::setlocale(LC_ALL, "en_US.utf8");
 +
    std::wcout.imbue(std::locale("en_US.utf8"));
 +
    std::wcout << L"Wide array contains: ";
 +
    for (std::size_t n = 0; n < std::size(to); ++n)
 +
        if (to[n])
 +
            std::wcout << to[n];
 +
        else
 +
            std::wcout << L"\\0";
 +
    std::wcout << L'\n';
 +
}
 +
|p=true
 +
|output=
 +
Wide array contains: नमस्ते\0Բարև\0
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | c/string/wide/dcl list wmemmove}}
+
{{dsc inc|cpp/string/byte/dsc strncpy}}
{{dcl list end}}
+
{{dsc inc|cpp/string/wide/dsc wmemmove}}
 +
{{dsc see c|c/string/wide/wmemcpy}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 11:17, 10 June 2023

Defined in header <cwchar>
wchar_t* wmemcpy( wchar_t* dest, const wchar_t* src, std::size_t count );

Copies exactly count successive wide characters from the wide character array pointed to by src to the wide character array pointed to by dest. If the objects overlap, the behavior is undefined. If count is zero, the function does nothing.

Contents

[edit] Parameters

dest - pointer to the wide character array to copy to
src - pointer to the wide character array to copy from
count - number of wide characters to copy

[edit] Return value

dest

[edit] Notes

This function's analog for byte strings is std::strncpy, not std::strcpy.

This function is not locale-sensitive and pays no attention to the values of the wchar_t objects it copies: nulls as well as invalid characters are copied too.

[edit] Example

#include <clocale>
#include <cwchar>
#include <iostream>
#include <iterator>
#include <locale>
 
int main(void)
{
    const wchar_t from1[] = L"नमस्ते";
    const wchar_t from2[] = L"Բարև";
    const std::size_t sz1 = std::size(from1);
    const std::size_t sz2 = std::size(from2);
    wchar_t to[sz1 + sz2];
 
    std::wmemcpy(to, from1, sz1); // copy from1, along with its null terminator
    std::wmemcpy(to + sz1, from2, sz2); // append from2, along with its null terminator
 
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    std::wcout << L"Wide array contains: ";
    for (std::size_t n = 0; n < std::size(to); ++n)
        if (to[n])
            std::wcout << to[n];
        else
            std::wcout << L"\\0";
    std::wcout << L'\n';
}

Possible output:

Wide array contains: नमस्ते\0Բարև\0

[edit] See also

copies a certain amount of characters from one string to another
(function) [edit]
copies a certain amount of wide characters between two, possibly overlapping, arrays
(function) [edit]
C documentation for wmemcpy