小编Her*_*les的帖子

在C++中复制数组的麻烦

我在C++中复制一个数组,这里是代码:

int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int *source = arr1;
size_t sz = sizeof(arr1) / sizeof(*arr1); // number of elements
int *dest = new int[sz];                // uninitialized elements
while (source != arr1 + sz)
    *dest++ = *source++; //  copy element and increment pointers

int *p = dest;
while (p != dest + sz) {
    cout << *p++ << endl;
}
Run Code Online (Sandbox Code Playgroud)

运行上述代码后,我得到了:

714124054
51734
9647968
9639960
0
0
0
0
0 …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
97
查看次数

标签 统计

c++ ×1