如何将一维数组复制到另一个二维数组,反之亦然的一部分? [英] How do I copy a one-dimensional array to part of another two-dimensional array, and vice-versa?

查看:904
本文介绍了如何将一维数组复制到另一个二维数组,反之亦然的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有三个阵列,CQueue(2D阵列),PQueue(2D阵列,相同数量的阵列作为CQueue的,但用含有两倍的值每个阵列)和CC(标准阵列是作为只要在CQueue数组)。我的希望的实现正在在CQueue特定阵列,复制它,使CC读取完全一样CQueue,因此,在PQueue相当于阵列上半年也读取相同

Basically, I have three arrays, CQueue (2D-array), PQueue (2D-array, the same number of arrays as the CQueue, but with each array containing twice as many values) and CC (standard array which is as long as an array in the CQueue). What I want to achieve is taking a specific array in the CQueue, copying it so that CC reads exactly the same as CQueue, and so that the first half of the equivalent array in the PQueue also reads the same.

我一直劝一个朋友,这似乎不错,但一直没有固定的问题使用的memcpy。我不知道这个问题是否在于内我可能使用的memcpy错误,或是否有另外一个问题在眼前。以下是code的相关部分的简化版本。

I've been advised to use memcpy by a friend, which seemed fine but hasn't fixed the problem. I don't know whether the problem lies within me potentially using memcpy incorrectly or whether there's another issue at hand. Following is a simplified version of the relevant part of the code.

int (main)
{
  int CQueue[numberOfArrays][halfSize]
  int PQueue[numberOfArrays][size]
  int CC[halfSize]

  for (i = 0; i < numberOfArrays]; i++)
  {
    memcpy (CC, CQueue[i], halfSize)
    memcpy (PQueue[i], CQueue[i], size)
  }
}

提供任何帮助将是非常美联社preciated,谢谢!

Any help offered would be much appreciated, thanks!

推荐答案

的第三个参数的memcpy 的量在字符的复制的(即字节在大多数平台)。所以,你需要做的是这样的:

The third argument to memcpy is the amount to copy in characters (i.e. bytes on most platforms). So you need to do something like:

memcpy(CC, CQueue[i], halfSize*sizeof(*CC));

这篇关于如何将一维数组复制到另一个二维数组,反之亦然的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆