12

I am coding on the Arduino platform and I am trying to write something that will concatenate/append byte arrays in C.

byte a[] = {a1, ..., an};
byte b[] = {b1, ..., bm};

byte c[] = a + b; // equivalent to {a1, ..., an, b1, ..., bm}

What is the best way to get the above result?

I tried searching online, however I have not had much luck. I saw another answer on SO highlighting the steps needed in order to do this however I could not follow them. They also say that there are libraries that deal with this kind of thing, however as I am on Arduino I am unsure whether or not these are totally available to me.

I understand there needs to be some sort of memory manipulation in order for this to work however I am new to these kinds of low level manipulations so they do not make too much sense to me. I have experience in higher languages (C#, Java and some C++).


I should also add: Can the same technique work for:

byte a[] = {a1, ..., an};
byte b[] = {b1, ..., bm};

a = a + b
6
  • Are the sizes fixed? Commented Feb 21, 2015 at 22:10
  • assuming byte is a char sized equivalent, byte c[sizeof(a) + sizeof(b]; and two memcpy calls would do it if that really is how a and b are declared. Commented Feb 21, 2015 at 22:11
  • @WhozCraig Yes. a and b are declared like this. However, I have edited the question to contain the case for when b is appended to a as well. Commented Feb 21, 2015 at 22:15
  • @Alizter that isn't going to happen unless a is initially sized to accommodate its data and space for appended data And a memcpy-type operation is still involved sooner or later. Regardless, the syntax you're using is definitely not congruent with the C language. Commented Feb 21, 2015 at 22:17
  • @WhozCraig Yes. I am aware that it is definitely not C. I have written it in a pseudo style to better explain my trouble. Commented Feb 21, 2015 at 22:19

2 Answers 2

13

There is no byte type in C. Unless it's some type definition, you could use unsigned char or some fixed type from <stdint.h> portably. Anyway, here is some solution:

#include <stdio.h>
#include <string.h>

int main(void) {
    unsigned char a[3+3] = {1, 2, 3}; // n+m
    unsigned char b[3]   = {4, 5, 6}; // m

    memcpy(a+3, b, 3); // a+n is destination, b is source and third argument is m

    for (int i = 0; i < 6; i++) {
        printf("%d\n", a[i]);
    }

    return 0;
}

Make sure that array a has room for at least n + m elements (here n = 3 and m = 3 as well) in order to avoid issues with array overflow (i.e. undefined behavior, that may crash yor program or even worse).

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

5 Comments

How would you make sure that a has enough room?
@Alizter unsigned char a[3 + 3] has room for the initial a + the appended b obviously.
@iharob Oh! I though there was some size I was missing. Of course.
So if we were to talk about an array c, assuming a and b are full, we would need to make c the 'length' of a + b and then copy using memcpy() into c b then a. What I am unsure of though is the a+3 syntax you have used. What does that mean?
@Alizter: Oh, my cognition only registered a=a+b case. In order of c=a+b you need to copy a to c, then b to c+n. The c+n works in the same way as a+3. The a "decays" (or more formaly is converted) to address of first element in array. By + operator with integer you are performing pointer arithmetic. The a+3 is equivalent to &a[3] so I believe you get an idea now.
0

If the sizes are known you can simply create a new byte array of that size and use simple loops to fill it.

1 Comment

No, no loops need to be involved.

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.