我正在学习如何使用指针复制字符数组。
我有以下的C++代码。我想做的是通过指针将一个数组(set1)传递到另一个指针数组(temp)中。
但是当我尝试打印出(temp)时,它和(set1)不相同。
通过指针将一个数组传递到另一个临时数组的指针。
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char set1[] = "ABC";
char* p = &set1[0];
int tempSize = 0;
char* temp = new char[256];
for (int i = 0; i < 3; i++)
{
*temp = *p;
cout << *temp; // ABC
++temp;
++tempSize;
++p;
}
cout << "\n";
for (int i = 0; i < tempSize; i++)
{
cout << temp[i]; // Why ABC is not printed?
}
delete [] temp;
return 0;
}