tags:

views:

102

answers:

4
byte test[4];
memset(test,0x00,4);

test[]={0xb4,0xaf,0x98,0x1a};

the above code is giving me an error expected primary-expression before ']' token. can anyone tell me whats wrong with this type of assignment?

+13  A: 

Arrays cannot be assigned. You can only initialize them with the braces.

The closest you can get, if you want to "assign" it later, is declaring another array and copying that:

const int array_size = 4;
char a[array_size] = {};  //or {0} in C.
char b[array_size] = {0xb4,0xaf,0x98,0x1a};
std::copy(b, b + array_size, a);

or using the array class from std::tr1 or boost:

#include <tr1/array>
#include <iostream>

int main()
{
    std::tr1::array<char, 4> a = {};

    std::tr1::array<char, 4> b = {0xb4,0xaf,0x98,0x1a};    
    a = b; //those are assignable

    for (unsigned i = 0; i != a.size(); ++i) {
        std::cout << a[i] << '\n';
    }
}
UncleBens
+1 for the nice edit.
Void
if i assign values using copy will there be scoping issues if i use it in the confines of an if statement? what i want to do is use one byte array in all the functions but have 4 other byte inputs. when i know which one i want to use i then copy to the generic byte array (i was going to put this int he if statement.)
djones2010
You will be *copying* the byte values, so no problem if the source array has a smaller scope.
UncleBens
if i wanted to confirm the copy went through fine is there a way to print out the values of this byte array?? how can i tell if the copy passed or not with proper values?
djones2010
+4  A: 

In addition to @UncleBens's correct answer, I want to note that this:

byte test[4];
memset(test,0x00,4);

Can be shortened to:

byte test[4] = { 0 };

This is the initialization syntax that you're trying to use. The language will fill up un-assigned spaces with 0, so you don't have to write { 0, 0, 0, 0 } (and so that, if your array length changes later, you don't have to add more).

Chris Lutz
Which can be further shortened to byte test[4] = {};, but I do prefer the {0} as well.
John Dibling
@John - It can, but only in C++. In C (which is what I do predominantly) you have to have at least one value.
Chris Lutz
`byte test[4] = { 0 };` always made me think that what was in the braces was getting assigned to all the elements (like in one of C++'s `std::vector` constructors).
Mike DeSimone
@Mike: Nope, you are just specifying the first element. All the remaining elements will be value-initialized, which in this case means set to 0.
John Dibling
+2  A: 

What Ben and Chris are saying is.

byte test[4]={0xb4,0xaf,0x98,0x1a};

If you want to do it at run time, you can use memcpy to do the job.

byte startState[4]={0xb4,0xaf,0x98,0x1a};
byte test[4];

memcpy(test, startState, sizeof(test));
EvilTeach
This works if the OP wants to have those values immediately, but not if they want to assign them later. But +1
Chris Lutz
Ya, good point Chris.Thank you.I added a technique to do that.
EvilTeach
basically i have like 5 byte arrays that i wanted to selectively use based on some initial data that i parse. So, i was thinking about setting up an array and then putting values in it which would never change i guess i would put const on it as well since it will never change.
djones2010
+4  A: 

In addition to @Chris Lutz's correct answer:

byte test[]={0xb4,0xaf,0x98,0x1a};

Note that you don't need to explicitly specify the array size in this case unless you want the array length to be larger than the number of elements between the brackets.

This only works if you're initializing the array when it is declared. Otherwise you'll have to initialize each array element explicitly using your favorite technique (loop, STL algorithm, etc).

Void
Also note that you can auto-determine the array size with: `size_t numTestElems = sizeof(test) / sizeof(byte);`
Mike DeSimone
@Mike - Or, since this is C++, `template <class T, size_t N> size_t array_size(T ( }` used as `size_t numTestElems = array_size(test);`
Chris Lutz
@Mike: I'd go with sizeof(test) / sizeof(test[0]). That way you don't have to worry about potential type mix-ups.
Void
@Chris: I've never seen that before (sadly :)). Very nice!
Void