C++:strcpy函数

基本用法

1.函数原型 char *strcpy(char *dest,const char *src)
2.从src地址开始且含有null结束符的字符串复制到以dest地址开始的字符串中,并返回指向dest的指针。通俗的讲就是将 src字符数组复制到dest数组中,如果dest数组本身有数据,会把src里的数据全部复制到dest中,如果dest中有数据小于src地址长度的将会被覆盖,而大于src长度的将保留。
3.注意事项:
dest的地址长度要足够大,不然会产生溢出。Dest的内存长度要大于等于src的内存长度。

原题

程序找错

#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
#pragma warning( disable : 4996) 
using namespace std;

void test1()
{
	char string[10];
	const char* str1 = "0123456789";
	strcpy(string, str1);
	printf(string);
}

int main()
{
	test1();
	system("pause");
    return 0;
}

错误
string开的空间为10,而str1的长度为11(算上字符串结束字符)

原文地址:https://www.cnblogs.com/wt-seu/p/12830022.html