4

This may be an easy question, and if so im sorry, but i couldn't find out how to do this. I've programmed in python before and hoped that this would work, but it don't.

int myArray1[] = {0, 2, 4, 5, 7, 9, 11};
int myArray2[] = {0, 2, 3, 5, 7, 8, 10};
int myArray3[] = {0, 3, 5, 6, 7, 10};

int newArray[] = myArray2;

I wanted to only have to change one place in my code, which of the first three arrays i want my new array to take values from. Do anyone have any suggestions on how to do this?

landigio
  • 141
  • 1
  • 1
  • 2
  • memcpy() is the C function for copying blocks of memory. If nobody has answered before I get back to a computer I shall write something up, providing you clarify whether you want to copy the contents or not, and whether the arrays are equal fixed length, different fixed lengths, or truly dynamic lengths. – CharlieHanson Jan 19 '16 at 19:34
  • I suggest you clarify if you want a copy of the contents, or merely make newArray refer to one of the existing arrays. Your wording which of the first three arrays i want my new array to take values from suggests you merely want to change a reference to one of the existing arrays. – Nick Gammon Jan 19 '16 at 21:12

1 Answers1

3

Make newArray be a pointer to whichever array you wish to work with. For example:

int myArray1[] = {0, 2, 4, 5, 7, 9, 11};
int myArray2[] = {0, 2, 3, 5, 7, 8, 10};
int myArray3[] = {0, 3, 5, 6, 7, 10};

int *newArray = myArray2;

// ... work with selected array, via newArray[] subscripting

Note, you may need to set an array-length variable at the same time as choosing the array.

For example:

int *newArray = myArray2;
int newSize = sizeof(myArray2)/sizeof myArray2[0];

// ... work with selected array

newArray = myArray3;
newSize = sizeof(myArray3)/sizeof myArray3[0];
// ... work with selected array

Alternately, add an array-end marker at the end of each array (eg, a negative number), or more elegantly, make all the arrays the same length.

If you want to make a new copy of the original array, such that modifying the new array won't change the old, use (eg) memcpy(). For example:

int newArray[7];
...
memcpy(newArray, myArray2, sizeof(myArray2));

Note that memcpy()'s size argument is given in bytes. Also, if you need to declare memcpy() [I don't recall whether it's automatically declared in the Arduino environment], add #include <string.h> up front.

James Waldby - jwpat7
  • 8,840
  • 3
  • 17
  • 32
  • 2
    But this doesn't copy the contents, it just creates a duplicate reference... – CharlieHanson Jan 19 '16 at 17:57
  • @CharlieHanson, that complies with an interpretation of the question consistent with Python's implementation, which produces a new reference to a list. Re Python's copy and deep copy see 8.17. copy — Shallow and deep copy operations – James Waldby - jwpat7 Jan 19 '16 at 19:08
  • Yet it does not comply with the title "Copy content of array". – CharlieHanson Jan 19 '16 at 19:10
  • @CharlieHanson, I'd instead say the title does not comply with the content of the post. We can hope OP clarifies. Anyway, will add memcpy note – James Waldby - jwpat7 Jan 19 '16 at 19:25
  • I'd instead say the content of the post does not comply with the title. Sic vita est. – CharlieHanson Jan 19 '16 at 19:32
  • memcpy(newArray, myArray2, sizeof(myArray2)); - I always like to take the size of the destination, not the source ... just in case. – Nick Gammon Jan 19 '16 at 21:14
  • @NickGammon, If source is myArray3 that will copy an extra word ... typically harmless, technically undefined. Of course, one could preface the memcpy with an if to test sizeof(source) <= sizeof(dest) – James Waldby - jwpat7 Jan 19 '16 at 22:43
  • Or something like: memcpy(newArray, myArray2, min (sizeof(myArray2), sizeof (newArray))); – Nick Gammon Jan 19 '16 at 23:39
  • Would there be much overhead in wrapping this in a simple class? I know there are a host of container classes in the C++ STL but they are all overkill, in this case at least. The ability to iterate and reference only within the bounds of the array would be a handy tool, and adding a copy function (a copy constructor, even?) could make using the array painless. Apart from the time taken to actually write the class, are there any drawbacks from an embedded point of view that would make this more harm than good? – CharlieHanson Jan 19 '16 at 23:44
  • Conceivably some sort of wrapper might run more slowly. I don't see any enormous disadvantages, except learning to use the class might take just as long as learning how to do the memcpy. :) – Nick Gammon Jan 20 '16 at 02:40