Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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'
share|improve this question
    
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 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. – EJP Jul 13 '13 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. – user1210456 Jul 13 '13 at 0:23
    
in c# it works like I tried it : stackoverflow.com/questions/549399/… how can I do this in c++? – user1210456 Jul 13 '13 at 0:28

3 Answers 3

up vote 2 down vote accepted

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}
};
share|improve this answer
    
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. – user1210456 Jul 13 '13 at 0: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 1:35
    
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

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

share|improve this answer
    
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. – user1210456 Jul 13 '13 at 1: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. – user1210456 Jul 13 '13 at 13:54

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];
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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