返回指向dest的指针。
/**********************
* C语言标准库函数strcpy的一种典型的工业级的最简实现
* 返回值:
* 返回目标串的地址。
* 对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。
* 参数:
* strDestination
* 目标串
* strSource
* 源串
***********************/
char *strcpy(char *strDestination, const char *strSource)
{
assert(strDestination!=NULL && strSource!=NULL);
char *strD=strDestination;
while ((*strDestination++=*strSource++)!='\0')
return strD;
}
strcpy函数,就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它只能假定缓冲足够大来容纳要拷贝的字符串。在程序运行时,这将导致不可预料的行为。 用strcpy_s就 可以避免这些不可预料的行为。
这个函数用两个参数、三个参数都可以,只要可以保证缓冲区大小。
三个参数时:
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
两个参数时:
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
例子:
#include "stdafx.h"
#include<iostream>
#include<string.h>
using namespace std;
void Test(void)
{
char *str1=NULL;
str1=new char[20];
char str[7];
strcpy_s(str1,20,"hello world");//三个参数
strcpy_s(str,"hello");//两个参数但如果:char *str=new char[7];会出错:提示不支持两个参数
cout<<"strlen(str1)"<<strlen(str1)<<"strlen(str)"<<strlen(str)<<endl;
printf(str1);printf("\n");
cout<<str<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
return 0;
}
C++标准库函数提供了字符和字符串的操作函数,并提供了其UNICODE版本,如:
char *strcpy(char *strDestination, const char *strSource);
wchar_t *wcscpy(wchar_t *strDestination, const wchar_t *strSource);
wcscpy()即为strcpy()的宽字符版本,与_T类似的,Visual C++提供了类似的同名函数:
#ifdef UNICODE
#define _tcscpy wcscpy
#else
#define _tcscpy strcpy
#endif
因此我们建议这样书写代码:
TCHAR src[] = _T("学习C++");
TCHAR dest[20];
_tcscpy(dest, src);
比如,在使用printf()的时候,我会尝试使用_tprintf()。
同样的版本问题一样会困扰着main()函数:
main( int argc, char *argv[ ], char *envp[ ]);
wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ]);
再来看_tmain()的定义:
#ifdef UNICODE
#define _tmain wmain
#define _tWinMain wWinMain
#else
#define _tmain main
#define _tWinMain WinMain
#endif
这就是为什么Win32控制台项目默认输出,提供一个_tmain()函数的缘故。
http://hi.baidu.com/drunkdream/item/1edb79f4391cb9b530c199d7
http://baike.baidu.com/view/1026861.htm
http://www.cnblogs.com/lancidie/archive/2011/02/17/1956671.html