How to copy a one-dimensional array to part of another two-dimensional array and vice versa?

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

I was advised to use memcpy as a friend who seemed fine but didn't fix the problem. I don't know if there is a problem in me, potentially using memcpy incorrectly or if there is another problem. The following is a simplified version of the corresponding piece of 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) } } 

Any help offered would be greatly appreciated, thanks!

0
source share
2 answers

The third memcpy argument 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)); 
+1
source

memcpy copies bytes, not int. You need to multiply the number of elements to copy by the size of each such element

 memcpy(dst, src, elems * sizeof elem); 

in your code

  memcpy (CC, CQueue[i], halfSize * sizeof *CC); memcpy (PQueue[i], CQueue[i], size * sizeof *PQueue[i]); 
0
source

Source: https://habr.com/ru/post/1483031/


All Articles