0

Hope my question is clear and relavent, new to Pointers... - Can I copy a whole portion of an array at once, by refering to the pointer to the location of the first slot in the array I want to begin copying from?

For example - Given an array : A [ 1,2,3,4,5,7,8,3,2,5,1,0,9] - I want to copy only the part of the array from the n'th slot on, into the beginning of the array B [0 0 0 ..... ] (B is of the same length of A).

Can I do it at once, using pointers instead of a loop? Something like - switching the pointer to the 1'st slot in B with the pointer to the n'th slot of A, and the n'th slot in B with the last one in A?

Thanks a lot on advance!

user1611107
  • 287
  • 1
  • 4
  • 13

2 Answers2

6

That's what memcpy is for.

memcpy(B, A + n, (N - n) * sizeof(A[0]));

where N is the number of elements in A. If A is really an array (not just a pointer to one), then N can be computed as sizeof(A) / sizeof(A[0]), so the call simplifies to

memcpy(B, A + n, sizeof(A) - n * sizeof(A[0]));

memcpy lives in <string.h>; its first argument is the destination of the copy, its second the source.

(I'm sorry, I don't really follow what kind of pointer trick you have in mind, so I can't comment on that.)

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Can you clearfy a bit more? I see the first entry is my new array, the second is the slot I begin copying from , what is the third? – user1611107 Oct 28 '12 at 12:53
  • 1
    @user1611107: the number of bytes to be copied. Btw., I forgot a factor of `sizeof(A[0])`, see updated answer. – Fred Foo Oct 28 '12 at 12:53
  • @user1611107: it's in the C standard library. How it's implemented is not specified by the standard, but you can assume it's heavily optimized, as copying bytes around is a very common operation in C. – Fred Foo Oct 28 '12 at 12:56
  • Got it, cheack it out now. Thanks :-) – user1611107 Oct 28 '12 at 12:57
0

I think I understand what you're asking. You can use pointers to set up your second array, but here is the problem with doing it that way:

int [] primary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int * secondary = primary + 5;

At this point, primary is { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, and secondary is { 6, 7, 8, 9, 0 }, but the problem is they both point to the same array in memory. So instead of having two independent copies, if you edit any of the elements of 'secondary', they are edited in 'primary' also.

secondary[2] = 10;
for(int i = 0; i < 10; ++i)
  cout << primary[i] << ' ';

This portion of code would now yield: 1 2 3 4 5 6 7 10 9 0

The correct way to do it would to either be setting up the new array, and looping through copying over the values, or using memcpy().

Edit:

//Rotate an array such that the index value is moved to the front of the array
null rotateArray( &int original[], int size, int index)
{
  int * secondary = new int[size]; //dynamically allocated array
  for(int i = 0; i < size; ++i)
  {
    secondary[i] = original[(i+index)%size];
  }
  original = secondary; //Move the original array's pointer to the new memory location
}

Some notes:

secondary[i] = original[(i+index)%size];

this is how I rotated the array. Say you had an original array of size 5, and you wanted the fourth element to be the new start (remember, elements are numbered 0-(size-1)):

i = 0;

secondary[0] = original[(0 + 3)%5]// = original[3]

i = 1;

secondary[1] = original[(1 + 3)%5]// = original[4]

i = 2;

secondary[2] = original[(2 + 3)%5]// = original[0]

...

The last part of the code:

original = secondary;

is a little bit questionable, as I don't remember whether or not c++ will try and clean up the used memory once the function is exited. A safer and 100% sure way to do it would be to loop through and copy the secondary array's elements into the original array:

for(int i = 0; i < size; ++i)
  original[i] = secondary[i];
Logan Nichols
  • 397
  • 1
  • 7
  • That is indeed what I meant, the truth is - I don't need to keep A, I want B to be a re-organized form of A. This is what I mean: Say A [ 1 2 3 4 5 ] than be will be B [ 3 4 5 1 2 ] --> The second part of A, now moved to the beginig, and the first part --> to the end. To your method, the secondary array is now a SMALLER array in size, or can I now add to it the first slots as I described? – user1611107 Oct 28 '12 at 13:25
  • @user1611107 Aah. The reason the second array is listed as smaller because it now points to the middle of the first array. if you want to keep all values of the array, just reorganize them, then this is not what you want. Instead, the easiest way is to do a customized copy into a secondary array. I'll add it to the answer above. – Logan Nichols Oct 28 '12 at 17:20
  • Thank you, I c what you mean, but than this is simply back to the strait-forward way of doing it, and from what I c so far, in this case Memcpy does seem to do it a bit faster (- or maybe it's just an illusion...). I havn't cheacked how it is built yet, but PSCICOLOGICALLY - it apears to save me a loop, so I'm happy :-) – user1611107 Oct 29 '12 at 08:11