jjjjj
jjjjj

Reputation: 73

copy all the array into new array without vectors C++

int led_p1[8] = {0x01, 0x02, 0x04, 0x08, 
                 0x10, 0x20, 0x40, 0x80};
int led_p2[8] = {0x81, 0x42, 0x24, 0x18,
                 0x18, 0x24, 0x42, 0x81};
int led_p3[8] = {0xFF, 0x00, 0xFF, 0x00,
                 0xFF, 0x00, 0xFF, 0x00};

int led_pattern[8] = {};
int pattern_idx = 0;



// select pattern for now
switch(pattern_idx)
{
    case 0: 
        led_pattern = led_p1;
        pattern_idx++;
        break;
    case 1: 
        led_pattern = led_p2;
        pattern_idx++;
        break;
    case 2: 
        led_pattern = led_p3;
        pattern_idx = 0;
        break;
}

I tried doing the above, I am going to loop round idx, copying a new sequence into the led_pattern, then loop round that, come back, get the next one.

I found a method to pass a whole array using a vector here; C++: copy array

But vectors (I believe) don't work on the compiler I am using (avr-gcc/c++)

How can I implement this within C++? I know in python the below would work, I can assign an array to another variable, and it "clones" it.

Thanks

Upvotes: 2

Views: 2030

Answers (6)

JoeFish
JoeFish

Reputation: 3100

If I'm reading the OP correctly, you do not want a deep copy, but want to switch what array is being used for the LEDs. So you would make a pointer that you can switch among arrays, like so:

int led_p1[8] = {0x01, 0x02, 0x04, 0x08, 
                 0x10, 0x20, 0x40, 0x80};
int led_p2[8] = {0x81, 0x42, 0x24, 0x18,
                 0x18, 0x24, 0x42, 0x81};
int led_p3[8] = {0xFF, 0x00, 0xFF, 0x00,
                 0xFF, 0x00, 0xFF, 0x00};

int *led_pattern = led_p1; //Starting point
int pattern_idx = 0;

// select pattern for now
switch(pattern_idx)
{
    case 0: 
        led_pattern = led_p1;
        pattern_idx++;
        break;
    case 1: 
        led_pattern = led_p2;
        pattern_idx++;
        break;
    case 2: 
        led_pattern = led_p3;
        pattern_idx = 0;
        break;
}

So now you can switch led_pattern to point at whichever led_p* array you're currently using.

Upvotes: 3

SwiftMango
SwiftMango

Reputation: 15294

To make a hard copy in the memory level, you might want to use the old C function: memcpy

memcpy (led_pattern,led_p1, 8 * sizeof(int));

8 should be replaced by array size if it is different.

Function explanation: www.cplusplus.com/reference/clibrary/cstring/memcpy/

#include <string.h> should also be used.(Edited as Prætorian said)

Upvotes: 1

Component 10
Component 10

Reputation: 10497

Do you really need to copy into led_pattern? Are you going to change the contents? If it's essentially const, why don't you declare led_pattern as a pointer to an int? That would be much more efficient as you'll just be pointing to one of the three existing arrays rather than copying them.

If not, use memcpy to copy from one of your three arrays into led_pattern

Upvotes: 4

Torsten Robitzki
Torsten Robitzki

Reputation: 2555

A usual approach would be to use std::copy(). std::copy takes two iterators that denote the start and the end of the sequence to copy and a third parameter, as the destination:

std::copy( &led_p1[ 0 ], &led_p1[ sizeof(led_p1) / sizeof(led_p1[ 0 ]) ], &led_pattern[ 0 ])

Where the expression sizeof(led_p1) / sizeof(led_p1[ 0 ]) evaluates to 8, the number of elements in your array. Instead of using 3 distinct array with you LED pattern, you could use an array with 3 arrays and iterate over this 3 arrays.

Upvotes: 2

matzahboy
matzahboy

Reputation: 3024

To make a deep copy, you have to copy every single value in the first array to the new array. Instead of just assigning the old pointer to the new one, do a for loop on the number of elements in the array and assign each one to the new copy.

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168716

Replace this line that doesn't do what you want:

led_pattern = led_p1;

With any one of these that will, in descending order of preference.

std::copy(led_p1, led_p1 + 8, led_pattern);

memcpy(led_pattern, led_p1, sizeof led_p1);

for(int i = 0; i < 8; i++) led_pattern[i] = led_p1[i];

Upvotes: 3

Related Questions