2

I'm kind of new to c++ and my question might have a pretty easy solution, but I couldn't figure it out by myself.

Let's say I have two byte arrays a and b. Each of them contains six bytes. Now I want to introduce a new array c which should contain a and b.

This is how I tried it:

byte a[] = {B11111111, B10010000, B10011000, B10010100, B10010010, B11110001};
byte b[] = {B11111111, B10000001, B10000001, B10000001, B10000001, B11111111};

byte c[2][6] = {{a},{b}};

The compiler gives me the following error:

invalid conversion from 'byte' to 'byte'
4
  • Never mind that you're confusing addition and multiplication (or maybe I should say unions and products), but the solution I'm envisaging uses several traits to extract array sizes and templates of variable numbers of index packs to initialize a single union array from arbitrary constituents. I think that might be a bit more involved than you were looking for. Commented Jul 13, 2013 at 0:19
  • Your question embodies a contradiction in terms. A byte array is an array of bytes. An array of byte arrays is an array of arrays. Commented Jul 13, 2013 at 0:20
  • So, the question should be "how to create an array of byte arrays". It seems I can't edit questions jet. But there must be a smple solution to this anyway. Commented Jul 13, 2013 at 0:23
  • in c# it works like I tried it : stackoverflow.com/questions/549399/… how can I do this in c++? Commented Jul 13, 2013 at 0:28

3 Answers 3

6

Raw arrays are a bit annoying. Use std::array instead:

using std::array;
array<byte,6> a = {B11111111, B10010000, B10011000, B10010100, B10010010, B11110001};
array<byte,6> b = {B11111111, B10000001, B10000001, B10000001, B10000001, B11111111};
array<array<byte,6>,2> c = {a, b};

std::array was introduced in

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

2 Comments

This sounds like a good solution but I'm wirting the code for an arduino and I get the following error: expected constructor, destructor, or type conversion before '<' token.
This solution is also a coorect suggestion. Arduino doesn't support std::array and std::vector. I marked the pointer solution as the right one because it's a good solution and it works for me. Anyway, using std::array might be a better soloution in some other cases. I will vote +1 as soon as my reputation allows it.
4

You could do it as follows:

byte a[] = {B11111111, B10010000, B10011000, B10010100, B10010010, B11110001};
byte b[] = {B11111111, B10000001, B10000001, B10000001, B10000001, B11111111};

byte* c[2] = {a,b};

But it would be cleaner to just do a multidimensional array directly:

byte c[2][6] = {
  {B11111111, B10010000, B10011000, B10010100, B10010010, B11110001},
  {B11111111, B10000001, B10000001, B10000001, B10000001, B11111111}
};

3 Comments

This works. Can you explain why it is cleaner to do it directly and what exactly does byte*. I have to create the multidimensional array later in the code because a and b represent bitmaps for letters and c is a word. Which word it is, should be variable.
byte* c[2] is an array holding 2 byte pointers. A byte[] array can be degraded into a byte* pointer. So c is merely pointing at the starting memory addresses of a and b, rather than making copies of them.
It is cleaner if you're simply declaring these at the top of your code somewhere, since it's all in one, simple object without any unnecessary intermediary objects (e.g. a and b). However, if you have a and b anyway and just want to hold them together, then the first solution makes more sense. As Remy explained, byte * c[2] is just an array of byte*, so each entry in c[] is a pointer - and hence an array - in itself. You can think of byte a[] as being the same as byte *a.
1

you have to do a for loop to copy the 2 arrays into the third,

for (int i = 0; i < 6; i++)
{
   c[0][i] = a[i];
   c[1][i] = b[i];
}

Comments

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.