Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

byte array assignment

Tags:

c++

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?

like image 248
djones2010 Avatar asked Feb 10 '10 21:02

djones2010


Video Answer


1 Answers

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';
    }
}
like image 116
UncleBens Avatar answered Sep 20 '22 16:09

UncleBens