Search code examples
c++avravr-gcc

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


Solution

  • 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