8
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?

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
djones2010
  • 369
  • 2
  • 5
  • 13

4 Answers4

17

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
  • 40,819
  • 6
  • 57
  • 90
  • 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 Feb 10 '10 at 22:01
  • You will be *copying* the byte values, so no problem if the source array has a smaller scope. – UncleBens Feb 10 '10 at 22:13
  • 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 Feb 10 '10 at 22:26
14

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
  • 28,120
  • 21
  • 85
  • 141
  • 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 Feb 10 '10 at 21:33
8

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).

Community
  • 1
  • 1
Void - Othman
  • 3,441
  • 18
  • 18
4

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).

Community
  • 1
  • 1
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
  • 1
    Which can be further shortened to byte test[4] = {};, but I do prefer the {0} as well. – John Dibling Feb 10 '10 at 21:27
  • 1
    @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 Feb 10 '10 at 21:34
  • `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 Feb 10 '10 at 21:34
  • 2
    @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 Feb 10 '10 at 21:37