請你能幫助我嗎? 如何將一個int數組的一部分複製到另一個int數組中?如何將int數組的一部分複製到C/C++中的另一個int數組中?
例子:
typedef struct part {
int * array;
} PART;
int array[] = {1,2,3,4,5,6,7,8,9};
PART out[] = new PART[3];
for (int i = 0; i < 3; i++)
{
memcpy((char *)array[i * 3], (char *)out[i].array, 3 * sizeof(int));
}
但是,這不工作... :(
除了下面的答案,你的使用'memcpy'可能是錯誤的。 memcpy的第一個參數是_destination_,而不是源。 – lijie 2010-12-10 19:03:29
我看到你的問題有C++標記,如果是的話,不要用「typedef struct」醜化你的代碼,不要使用memcpy,C++有std :: copy。僅爲宏使用ALL_CAPS名稱也是一個好主意。 – 2010-12-10 19:08:33