Error: Initialization with "{. .. }" Expected for Aggregate Object - C++

Error: Initialization with "{. .. }" Expected for Aggregate Object - C++
struct test
{
    unsigned int test1;
    unsigned char test2[4096];
    unsigned int test3;
} foo

struct foobar
{
unsigned char data[4096];
}

if i want to access the struct, i say foo.test1, foo.test2[4096], etc.. however, when I wish to return the data present in foo.test2 in the following manner

pac.datafoo = foo.test2[4096];

unsigned char data[4096] =  pac.datafoo;

this is the error I get:

error: initialization with "{...}" expected for aggregate object

what is the mistake i'm doing?

8

3 Answers

You need to learn the array initialization method. It's NOT simply assigned as the single variable.

Some examples:

int arrayone[3] = {0}; // assign all items with 0

int arraytwo[3] = {1, 2, 3 }; // assign each item with 1, 2 and 3

int arraythree[3]; // assign arraythree with arraytwo
for (int i = 0; i < 3; ++i) {
    arraythree[i] = arraytwo[i];
}
1

add ";" at the end of the struct.

struct test
{
    unsigned int test1;
    unsigned char test2[4096];
    unsigned int test3;
} foo ;

struct foobar
{
unsigned char data[4096];
} ;
unsigned char * data;

  data = pac.datafoo;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.