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'
Troyen
  • 1,398
  • 2
  • 23
  • 37
LonnyT
  • 590
  • 1
  • 8
  • 21
  • 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. – Kerrek SB Jul 13 '13 at 00: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. – user207421 Jul 13 '13 at 00: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. – LonnyT Jul 13 '13 at 00:23
  • in c# it works like I tried it : http://stackoverflow.com/questions/549399/c-sharp-creating-an-array-of-arrays how can I do this in c++? – LonnyT Jul 13 '13 at 00:28

3 Answers3

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

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 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. – LonnyT Jul 13 '13 at 01:24
  • 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. – LonnyT Jul 13 '13 at 13:54
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}
};
Skipper E
  • 96
  • 1
  • 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. – LonnyT Jul 13 '13 at 00:52
  • `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. – Remy Lebeau Jul 13 '13 at 01:35
  • 1
    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. – Skipper E Jul 13 '13 at 17:17
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];
}
aah134
  • 860
  • 12
  • 25