3

I have three byte arrays. I first change their values during runtime and after that I want to combine them into one bigger 4th array.

byte a1[] = { 0x01, 0x00, 0x01 };
byte a2[] = { 0x00, 0x11, 0x01 };
byte a3[] = { 0x11, 0x00, 0x01 };

byte c1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

I first tried this, but it did not compile.

c1 = a1 + a2 + a3;

I also tried strcat() and I did got a error.

2
  • the answer depends on what you meant by "concatenate". you may be able to clarify by showing what you expect to be in c1[].
    – dannyf
    Commented Jan 20, 2017 at 14:48
  • 1
    If any of the answers solved your problem, you should accept it.
    – gre_gor
    Commented Jan 21, 2017 at 21:59

3 Answers 3

6

You can concatenate with memcpy. You just need to set the pointer at the right place inside the c1 array.

memcpy(c1, a1, sizeof(a1));
memcpy(c1+sizeof(a1), a2, sizeof(a2));
memcpy(c1+sizeof(a1)+sizeof(a2), a3, sizeof(a3));
1
  • 1
    They should also be careful that they use this method (with sizeof(), I mean) only when in the same scope where the arrays are declared. If the pointers to the arrays are passed to a function concat(), for instance, this will fail since sizeof() would return the pointer size and not the array size. Commented Jan 23, 2017 at 21:59
1

You can do it like this

byte c1[] = {0x01, 0x00, 0x01, 0x00, 0x11, 0x01,0x11, 0x00, 0x01 };
byte *a1 = c1;
byte *a2 = c1+3;
byte *a3 = c1+6;

Note that This does not make a copy byt you can access the C1 and a1 a2 and a3 as you liked.

4
  • I have a problem with jantje answer. As any array (c1) is a pointer to the array. So when you attempt to byte a1[] = c1; You are attempting to place the address of c1 into the 'byte'. More likely without an index attempting to change the location of a1. This will not compile. All of these are doing just what gre_gor suggested (memcopy) and is the accepted way of copying or moving data. :) Commented Jan 23, 2017 at 14:09
  • You are right on byte a1[] = c1; I changed the answer. You are not right on stating this is the same as gre_gor suggested. In my case only 9 bytes of memory are used for data (I don't count the pointers). In other words there is only 1 copy of the data where in gre_gor there are 2 copies. Note that the question is very unclear about what is needed so for now we don't know a copy or simple reference is needed.
    – jantje
    Commented Jan 23, 2017 at 15:08
  • My suggestion about gre_gor was that it was the proper way to do it. Any changes will be fixed during a compile, your code will not. gilhad is virtually the same. This new version will not compile either as you didn't change the problem. Also, his code moves it from a to c, the opposite of yours, but the original source has the same value as does the destination. Where is the 'other' copy? But you are correct, more information is needed for a good answer. Commented Jan 23, 2017 at 23:01
  • This is really a comment, not an answer. With a bit more reputation, you will be able to post comments. For the moment I've added the comment for you, and I'm going to delete this answer. Thanks for your understanding and cooperation!
    – Nick Gammon
    Commented Jan 24, 2017 at 4:40
0
byte *src;
byte *tgt;
tgt=c1;
src=a1;
for (int i=0;i<3;i++) *(tgt++)=*(src++);
src=a2;
for (int i=0;i<3;i++) *(tgt++)=*(src++);
src=a3;
for (int i=0;i<3;i++) *(tgt++)=*(src++);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.