The difference between wcscpy wcscpy_s strcpy strcpy_s

Source: Internet
Author: User

Prototype declaration: extern Char *strcpy (char *dest,const char *src);

Header file: string.h

Function: Assigns a string with a null terminator starting from the SRC address to an address space starting with dest

Description: The memory areas referred to by SRC and dest cannot overlap and dest must have sufficient space to accommodate the SRC string.

Returns a pointer to the dest.

/************************** * C language standard library function strcpy A typical industry-class minimalist implementation * return value: * Returns the address of the destination string.  * For exceptions, the ANSI-C99 standard is not defined, so the implementation determines the return value, usually null. * Parameters: * strdestination * Target String * strsource * source string **************************/
Char*strcpy (Char*strdestination,Const Char*strsource) {Assert (strdestination!=null && strsource!=NULL); Char*strd=strdestination; while((*strdestination++=*strsource++)! =' /')returnStrD;}

The strcpy function, like the Get function, has no method to guarantee a valid buffer size, so it can only assume that the buffer is large enough to hold the string to be copied. This leads to unpredictable behavior when the program is running. These unpredictable behaviors can be avoided by strcpy_s.
This function can be used with two parameters and three parameters, as long as the buffer size can be guaranteed.
Three parameters:
errno_t strcpy_s (
Char *strdestination,
size_t numberOfElements,
const CHAR *strsource
);
Two parameters:
errno_t strcpy_s (
char (&strdestination) [size],
const CHAR *strsource
); C + + only


Example:

#include"stdafx.h"#include<iostream>#include<string.h>using namespacestd;voidTest (void){    Char*str1=NULL; STR1=New Char[ -]; Charstr[7]; strcpy_s (STR1, -,"Hello World");//three parametersstrcpy_s (str,"Hello");//two parameters but if: char *str=new char[7]; Error: Prompt does not support two parameterscout<<"strlen (str1)"<<strlen (STR1) <<"strlen (str)"<<strlen (str) <<Endl; printf (str1);p rintf ("\ n"); cout<<str<<Endl;} int_tmain (intARGC, _tchar*argv[])    {Test (); return 0;}

The C + + standard library function provides manipulation functions for characters and strings, and provides its Unicode version, such as:

Char *strcpy (char *strdestination, const char *strsource);
wchar_t *wcscpy (wchar_t *strdestination, const wchar_t *strsource);

wcscpy () is a wide-character version of strcpy (), similar to _t, Visual C + + provides similar functions with the same name:

#ifdef UNICODE
#define _TCSCPY wcscpy
#else
#define _TCSCPY strcpy
#endif

So we recommend writing code like this:

TCHAR src[] = _t ("Learning C + +");
TCHAR DEST[20];

_tcscpy (dest, SRC);

For example, when you use printf (), I try to use _tprintf ().

The same version problem will bother the main () function:

Main (int argc, char *argv[], char *envp[]);
wmain (int argc, wchar_t *argv[], wchar_t *envp[]);

Then look at the definition of _tmain ():

#ifdef UNICODE
#define _tmain WMAin
#define _tWinMain wWinMain
#else
#define _tmain Main
#define _tWinMain WinMain
#endif
This is why the default output of the WIN32 console project provides a _tmain () function.

The difference between wcscpy wcscpy_s strcpy strcpy_s

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.