以下のコード(およびコメント)を参照してください。
私はarray = newArrayを作成しようとしています。
C++でこれを行う正しい構文は何ですか?
memoryCopy()、
でNEWARRAYは、このように私がしたように要素ごとのそれをすることなく、アレイにNEWARRAYをコピーするように、私は同じメモリ位置にアレイを指すようにしようとしていdeepCopy()配列全体をコピーする方法(要素ごとではなく)
#include <iostream>
using namespace std;
void deepCopy(int array[], int size);
void memoryCopy(int *array, int size);
void show(int array[], int size);
int main()
{
int array[] = {1,1,1};
show(array,3); // shows array [] = {1,1,1}
deepCopy(array, 3);
show(array,3); // shows array [] = {0,0,0}
memoryCopy(array, 3);
show(array,3); // shows array [] = {5,0,0}
// I need the above to show {5, 5, 5}^^
// How can I do this in memoryCopy() using pointers?
}
void memoryCopy(int *array, int size)
{
int newArray[] = {5,5,5};
// I need to make array = newArray ...
// ... but without copying it over element by element
memcpy(array,newArray, 3); // <--???
}
void deepCopy(int array[], int size)
{
int newArray [] = {0,0,0};
for (int i = 0; i < size; i++)
array[i] = newArray[i];
}
void show(int array[], int size)
{
int i;
cout << "array [] = {";
for (i = 0; i < size-1; i++)
cout << array[i] << ",";
cout << array[i] << "}" << endl;
}
を働いていた方法です。 –
?私が必要とするのは、これを行う構文の例です。 – nulldreamer
'memcpy(array、newArray、3 * sizeof(int));'で確認できますか? –