2
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

1
  • I don't understand; why can't you do led_pattern[0] = led_p1[0] .. etc? Commented Jul 17, 2012 at 20:53

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

I think I may try this along with the others, as to be honest, yes I do just want to use and not modify the values.Thanks
+1 for identifying X, not solving Y, in the XY problem.
Great read on the XY problem, never heard of it. You learn something new every day! (well may two things in my case!) Thanks everyone.
3

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];

14 Comments

Or better yet, use Boost.Array which has proper copy semantics out of the box.
Or better, better yet, use std::array if your compiler supports it.
Although, if "vectors don't work on the compiler I am using," it is kind of hard to know what will.
It's possible that vectors don't work because exceptions are disabled; if so, array<> should still be fine.
@texasbruce - S'okay, mistakes happen. I hope I didn't sound too harsh. You should know, however, that you'll get the same answer in C.
|
3

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.

1 Comment

+1 for identifying OP's underlying problem, rather than (merely) answering his question.
2

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.

2 Comments

FYI: if you have C++11 available, the first two args can be std::begin(led_p1), std::end(led_p1).
@Robᵩ end even, if you do not have C++11 available, thous functions are so handy, that I've putt them in every tool box on every project I've worked on ;-) It's just a one liner each.
1

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.

Comments

1

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)

1 Comment

If you #include <cstring> you should qualify function call as std::memcpy. To call it as memcpy, include <string.h> instead. Or best of all, since the question is tagged C++, use std::copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.