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.

William Roy
  • 515
  • 4
  • 10
  • 22

3 Answers3

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));
gre_gor
  • 1,682
  • 4
  • 18
  • 28
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.

jantje
  • 1,392
  • 1
  • 8
  • 16
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++);
gilhad
  • 1,466
  • 2
  • 11
  • 20