Difference between revisions of "c/string/wide/wcscpy"
From cppreference.com
LittleFlower (Talk | contribs) (templatization) |
m (fmt.) |
||
Line 1: | Line 1: | ||
− | {{c/title| wcscpy|wcscpy_s}} | + | {{c/title|wcscpy|wcscpy_s}} |
{{c/string/wide/navbar}} | {{c/string/wide/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | wchar.h}} | + | {{dcl header|wchar.h}} |
{{dcl rev begin|num=1}} | {{dcl rev begin|num=1}} | ||
− | {{dcl | since=c95 | until=c99 | | + | {{dcl|since=c95|until=c99| |
− | wchar_t *wcscpy( wchar_t *dest, const wchar_t *src ); | + | wchar_t* wcscpy( wchar_t* dest, const wchar_t* src ); |
}} | }} | ||
− | {{dcl | since=c99 | | + | {{dcl|since=c99| |
− | wchar_t *wcscpy( wchar_t *restrict dest, const wchar_t *restrict src ); | + | wchar_t* wcscpy( wchar_t* restrict dest, const wchar_t* restrict src ); |
}} | }} | ||
{{dcl rev end}} | {{dcl rev end}} | ||
− | {{dcl |num=2| since=c11 | | + | {{dcl|num=2|since=c11| |
− | errno_t wcscpy_s( wchar_t *restrict dest, rsize_t destsz, | + | errno_t wcscpy_s( wchar_t* restrict dest, rsize_t destsz, |
− | const wchar_t *restrict src ); | + | const wchar_t* restrict src ); |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Copies the wide string pointed to by {{ | + | @1@ Copies the wide string pointed to by {{c|src}} (including the terminating null wide character) to wide character array pointed to by {{c|dest}}. The behavior is undefined if the {{c|dest}} array is not large enough. The behavior is undefined if the strings overlap. |
@2@ Same as {{v|1}}, except that it may clobber the rest of the destination array with unspecified values and that the following errors are detected at runtime and call the currently installed [[c/error/set_constraint_handler_s|constraint handler]] function: | @2@ Same as {{v|1}}, except that it may clobber the rest of the destination array with unspecified values and that the following errors are detected at runtime and call the currently installed [[c/error/set_constraint_handler_s|constraint handler]] function: | ||
− | :* {{ | + | :* {{c|src}} or {{c|dest}} is a null pointer |
− | :* {{ | + | :* {{c|destsz}} is zero or greater than {{c|RSIZE_MAX / sizeof(wchar_t)}} |
− | :* {{ | + | :* {{c|destsz}} is less or equal {{c|wcsnlen_s(src, destsz)}}, in other words, truncation would occur |
:* overlap would occur between the source and the destination strings | :* overlap would occur between the source and the destination strings | ||
:{{c/ext1 availability|wcscpy_s}} | :{{c/ext1 availability|wcscpy_s}} | ||
Line 27: | Line 27: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | dest | pointer to the wide character array to copy to}} | + | {{par|dest|pointer to the wide character array to copy to}} |
− | {{par | src | pointer to the null-terminated wide string to copy from}} | + | {{par|src|pointer to the null-terminated wide string to copy from}} |
− | {{par | destsz | maximum number of characters to write, typically the size of the destination buffer}} | + | {{par|destsz|maximum number of characters to write, typically the size of the destination buffer}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | @1@ returns a copy of {{ | + | @1@ returns a copy of {{c|dest}} |
− | @2@ returns zero on success, returns non-zero on error. Also, on error, writes {{c|L'\0'}} to {{c|dest[0]}} (unless {{ | + | @2@ returns zero on success, returns non-zero on error. Also, on error, writes {{c|L'\0'}} to {{c|dest[0]}} (unless {{c|dest}} is a null pointer or {{c|destsz}} is zero or greater than {{c|RMAX_SIZE / sizeof(wchar_t)}}). |
===Example=== | ===Example=== | ||
− | {{example | code= | + | {{example |
− | + | |code= | |
− | + | ||
#include <locale.h> | #include <locale.h> | ||
+ | #include <stdio.h> | ||
+ | #include <wchar.h> | ||
int main(void) | int main(void) | ||
{ | { | ||
− | wchar_t *src = L"犬 means dog"; | + | wchar_t* src = L"犬 means dog"; |
// src[0] = L'狗' ; // this would be undefined behavior | // src[0] = L'狗' ; // this would be undefined behavior | ||
− | wchar_t dst[wcslen(src) + 1]; // +1 | + | wchar_t dst[wcslen(src) + 1]; // +1 for the null terminator |
wcscpy(dst, src); | wcscpy(dst, src); | ||
dst[0] = L'狗'; // OK | dst[0] = L'狗'; // OK | ||
Line 53: | Line 54: | ||
printf("src = %ls\ndst = %ls\n", src, dst); | printf("src = %ls\ndst = %ls\n", src, dst); | ||
} | } | ||
− | + | |output= | |
src = 犬 means dog | src = 犬 means dog | ||
dst = 狗 means dog | dst = 狗 means dog | ||
Line 59: | Line 60: | ||
===References=== | ===References=== | ||
+ | {{ref std c23}} | ||
+ | {{ref std|section=7.29.4.1.2|title=The wcscpy function|p=TBD}} | ||
+ | {{ref std|section=K.3.9.2.1.1|title=The wcscpy_s function|p=TBD}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c17}} | ||
+ | {{ref std|section=7.29.4.1.2|title=The wcscpy function|p=TBD}} | ||
+ | {{ref std|section=K.3.9.2.1.1|title=The wcscpy_s function|p=TBD}} | ||
+ | {{ref std end}} | ||
{{ref std c11}} | {{ref std c11}} | ||
− | {{ref std | section=7.29.4.1.2 | + | {{ref std|section=7.29.4.1.2|title=The wcscpy function|p=430}} |
− | {{ref std | section=K.3.9.2.1.1 | title=The wcscpy_s function | p=639}} | + | {{ref std|section=K.3.9.2.1.1|title=The wcscpy_s function|p=639}} |
+ | {{ref std end}} | ||
{{ref std c99}} | {{ref std c99}} | ||
− | {{ref std | section=7.24.4.1.2 | title=The wcscpy function | p=376}} | + | {{ref std|section=7.24.4.1.2|title=The wcscpy function|p=376}} |
{{ref std end}} | {{ref std end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | c/string/wide/dsc wcsncpy}} | + | {{dsc inc|c/string/wide/dsc wcsncpy}} |
− | {{dsc inc | c/string/wide/dsc wmemcpy}} | + | {{dsc inc|c/string/wide/dsc wmemcpy}} |
− | {{dsc inc | c/string/byte/dsc strcpy}} | + | {{dsc inc|c/string/byte/dsc strcpy}} |
− | {{dsc see cpp | cpp/string/wide/wcscpy}} | + | {{dsc see cpp|cpp/string/wide/wcscpy}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 14:12, 13 November 2023
Defined in header <wchar.h>
|
||
(1) | ||
wchar_t* wcscpy( wchar_t* dest, const wchar_t* src ); |
(since C95) (until C99) |
|
wchar_t* wcscpy( wchar_t* restrict dest, const wchar_t* restrict src ); |
(since C99) | |
errno_t wcscpy_s( wchar_t* restrict dest, rsize_t destsz, const wchar_t* restrict src ); |
(2) | (since C11) |
1) Copies the wide string pointed to by src (including the terminating null wide character) to wide character array pointed to by dest. The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap.
2) Same as (1), except that it may clobber the rest of the destination array with unspecified values and that the following errors are detected at runtime and call the currently installed constraint handler function:
- src or dest is a null pointer
- destsz is zero or greater than RSIZE_MAX / sizeof(wchar_t)
- destsz is less or equal wcsnlen_s(src, destsz), in other words, truncation would occur
- overlap would occur between the source and the destination strings
- As with all bounds-checked functions,
wcscpy_s
is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <wchar.h>.
Contents |
[edit] Parameters
dest | - | pointer to the wide character array to copy to |
src | - | pointer to the null-terminated wide string to copy from |
destsz | - | maximum number of characters to write, typically the size of the destination buffer |
[edit] Return value
1) returns a copy of dest
2) returns zero on success, returns non-zero on error. Also, on error, writes L'\0' to dest[0] (unless dest is a null pointer or destsz is zero or greater than RMAX_SIZE / sizeof(wchar_t)).
[edit] Example
Run this code
#include <locale.h> #include <stdio.h> #include <wchar.h> int main(void) { wchar_t* src = L"犬 means dog"; // src[0] = L'狗' ; // this would be undefined behavior wchar_t dst[wcslen(src) + 1]; // +1 for the null terminator wcscpy(dst, src); dst[0] = L'狗'; // OK setlocale(LC_ALL, "en_US.utf8"); printf("src = %ls\ndst = %ls\n", src, dst); }
Output:
src = 犬 means dog dst = 狗 means dog
[edit] References
- C23 standard (ISO/IEC 9899:2024):
- 7.29.4.1.2 The wcscpy function (p: TBD)
- K.3.9.2.1.1 The wcscpy_s function (p: TBD)
- C17 standard (ISO/IEC 9899:2018):
- 7.29.4.1.2 The wcscpy function (p: TBD)
- K.3.9.2.1.1 The wcscpy_s function (p: TBD)
- C11 standard (ISO/IEC 9899:2011):
- 7.29.4.1.2 The wcscpy function (p: 430)
- K.3.9.2.1.1 The wcscpy_s function (p: 639)
- C99 standard (ISO/IEC 9899:1999):
- 7.24.4.1.2 The wcscpy function (p: 376)
[edit] See also
(C95)(C11) |
copies a certain amount of wide characters from one string to another (function) |
(C95)(C11) |
copies a certain amount of wide characters between two non-overlapping arrays (function) |
(C11) |
copies one string to another (function) |
C++ documentation for wcscpy
|