简体   繁体   中英

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?

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];
}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM