Namespaces
Variants
Views
Actions

Difference between revisions of "c/string/wide/wcscpy"

From cppreference.com
< c‎ | string‎ | wide
m (c95)
(_s, including DR 433)
Line 1: Line 1:
{{c/title| wcscpy}}
+
{{c/title| wcscpy|wcscpy_n}}
 
{{c/string/wide/navbar}}
 
{{c/string/wide/navbar}}
{{ddcl | header=wchar.h | notes={{mark since c95}}|
+
{{dcl begin}}
 +
{{dcl header | wchar.h}}
 +
{{dcl rev begin|num=1}}
 +
{{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 |
 +
wchar_t *wcscpy( wchar_t *restrict dest, const wchar_t *restrict src );
 +
}}
 +
{{dcl rev end}}
 +
{{dcl |num=2| since=c11 |
 +
errno_t wcscpy_s( wchar_t *restrict dest, rsize_t destsz,
 +
                  const wchar_t *restrict src );
 +
}}
 +
{{dcl end}}
  
Copies the wide string pointed to by {{tt|src}} (including the terminating null wide character) to wide character array pointed to by {{tt|dest}}.
+
@1@ Copies the wide string pointed to by {{tt|src}} (including the terminating null wide character) to wide character array pointed to by {{tt|dest}}. The behavior is the {{tt|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:
If the strings overlap, the behavior is undefined.
+
:* {{tt|src}} or {{tt|dest}} is a null pointer
 +
:* {{tt|destsz}} is zero or greater than {{c|RSIZE_MAX / sizeof(wchar_t)}}
 +
:* {{tt|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
 +
:As with all bounds-checked functions, {{tt|wcscpy_s}} is only guaranteed to be available if {{c|__STDC_LIB_EXT1__}} is defined by the implementation and if the user defines {{c|__STDC_WANT_LIB_EXT1__}} to the integer constant {{c|1}} before including {{tt|wchar.h}}.
  
 
===Parameters===
 
===Parameters===
Line 13: Line 29:
 
{{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 end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
{{tt|dest}}
+
@1@ returns a copy of {{tt|dest}}
 +
@2@ returns zero on success, returns non-zero on error. Also, on error, writes {{c|L'\0'}} to {{c|dest[0]}} (unless {{tt|dest}} is a null pointer or {{tt|destsz}} is zero or greater than {{c|RMAX_SIZE / sizeof(wchar_t)}}).
  
 
===Example===
 
===Example===
{{example
+
{{example | code=
|
+
#include <wchar.h>
  | code=
+
#include <stdio.h>
 +
#include <locale.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 to accommodate 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=
 
  | output=
 +
猫 means dog
 +
狗 means dog
 
}}
 
}}
  
Line 29: Line 62:
 
{{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 see cpp | cpp/string/wide/wcscpy}}
 
{{dsc see cpp | cpp/string/wide/wcscpy}}
 
{{dsc end}}
 
{{dsc end}}

Revision as of 08:37, 23 February 2015

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 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

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

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)).

Example

#include <wchar.h>
#include <stdio.h>
#include <locale.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 to accommodate 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:

猫 means dog
狗 means dog

See also

copies a certain amount of wide characters from one string to another
(function) [edit]
copies a certain amount of wide characters between two non-overlapping arrays
(function) [edit]
copies one string to another
(function) [edit]