关于strncpy函数源代码 请问我的这个代码错在哪?
运行出来时乱码#include <iostream>
using namespace std;
char *mystrncpy(char *str2, const char *str1, int n)
{
int i = 0;
char *dest = str2;
while(*str1 != '\0' && i < n)
{
*str2++ = *str1++;
i++;
}
return dest;
}
void main()
{
char *p = "abcde";
char *q = new char(20);
char *w = mystrncpy(q,p,3);
cout << w << endl;
system("pause");
}
为什么下面这段代码也是错的?
#include <iostream>
using namespace std;
void main()
{
char *p = new char[20];
char *q = "abcde";
strncpy(p, q, sizeof(q));
cout << p << endl;
system("pause");
}
[ 本帖最后由 lateraware 于 2012-2-23 23:31 编辑 ]