用strncpy时字符数组和字符指针有着怎样的区别?
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char ch1[5];
char* ch2;
char* ch3 = "12345678";
strncpy(ch1,ch3,5);//ch1输出结果:12345&^% (注:后面几个是乱码)
strncpy(ch3,ch2,5);//ch3输出结果:12345
cout << ch1 <<endl;
cout << ch3 <<endl;
return 0;
}
能解释一下strncpy的运行结果么??
int main()
{
char ch1[5],ch5[5];
char ch2[11],ch6[11];
char* ch3 = new char[6];
char* ch7 = new char[6];
char* ch4 = new char[11];
char* ch8 = new char[11];
char* ch9 = new char[6];
char* ch = "12345678";
//strcpy(ch1,ch); cout << 1 << ch1 << endl;
//strcpy(ch2,ch); cout << 2 << ch2 << endl;
//strcpy(ch3,ch); cout << 3 << ch3 << endl;
//strcpy(ch4,ch); cout << 4 << ch4 << endl; //上面四行结果均是12345678
strncpy(ch5,ch,5); cout << 5 << ch5 << endl; //512345后面乱码
strncpy(ch6,ch,5); cout << 6 << ch6 << endl; //612345后面乱码
strncpy(ch7,ch,5); cout << 7 << ch7 << endl; //712345后乱码
strncpy(ch7,ch,6); cout << 7 << ch7 << endl; //7123456后乱码
strncpy(ch8,ch,5); cout << 8 << ch8 << endl; //812345后面乱码
strncpy(ch9,ch,5); cout << 9 << ch9 << endl; //912345后面乱码
system("pause");
return 0;
}
大话西游666
慕的地10843
相关分类