错误:聚合对象的初始化为“{...}” - c ++

时间:2014-02-21 02:55:51

标签: c++ data-structures

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

struct foobar
{
unsigned char data[4096];
}

如果我想访问结构,我说foo.test1,foo.test2 [4096]等。 但是,当我希望以下列方式返回foo.test2中的数据时

pac.datafoo = foo.test2[4096];

unsigned char data[4096] =  pac.datafoo;

这是我得到的错误:

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

我在做什么错误?

3 个答案:

答案 0 :(得分:3)

您需要学习数组初始化方法。它不仅仅被指定为单个变量。

一些例子:

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 :(得分:3)

添加“;”在结构的末尾。

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

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

答案 2 :(得分:0)

unsigned char * data;

  data = pac.datafoo;