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?
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?
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';
}
}
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));
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).
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).