初始化包含另一个数组的元素数组 [英] initialize an array of elements that contain another array

查看:69
本文介绍了初始化包含另一个数组的元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下内容:

struct array1

{

int id;

char * name;

};


struct array2

{

int id;

char * name;

array1 * myArray;

};


现在,我尝试通过初始化创建一个array2数组:

array2 collection [] =

{

10," first",

{1,first.first},

20,second,

{2," second.second" }
};


编译器生成以下类似错误:

错误:类型为1的值不能用于初始化

类型的实体array1 *


如何初始化上面的内容。 Thx


I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};

Now, I try to create an array of array2 by initialization:
array2 collection[] =
{
10, "first",
{1, "first.first"},
20, "second",
{2, "second.second"}
};

compiler generates following similar error:
error: a value of type "1" cannot be used to initialize an entity of
type "array1 *"

How am I going to initialize something like above. Thx

推荐答案

-----开始PGP签名消息-----

哈希: SHA1

wenm ... @ yahoo.com写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
wenm...@yahoo.com wrote:

hi,

我有以下内容:

struct array1

{

int id;

char * name;

} ;


struct array2

{

int id;

char * name;

array1 * myArray;

};


现在,我尝试通过初始化创建一个array2数组:

array2 collection [] =

{

10," first",

{1," first.first"},

20,second,

{2," second.second"}

};

编译器生成以下类似错误:

错误:类型为1的值不能用于初始化

类型的实体array1 *


如何初始化上面的内容。 Thx

I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};

Now, I try to create an array of array2 by initialization:
array2 collection[] =
{
10, "first",
{1, "first.first"},
20, "second",
{2, "second.second"}
};

compiler generates following similar error:
error: a value of type "1" cannot be used to initialize an entity of
type "array1 *"

How am I going to initialize something like above. Thx



您将为您的array1条目显式地使用malloc()空间,并且

使用malloc填充array2中的myArray指针( )ed值。


您将在程序逻辑中执行此操作。


您不会*使用初始化程序执行此操作。


HTH

- -

Lew Pitcher

----- BEGIN PGP签名-----

版本:GnuPG v1.4.3(MingW32) - WinPT 0.11.12

iD8DBQFEzltYagVFX4UWr64RAh + EAJ0XihKh3M + dhmO3sAad8t smGutOFACgq / UR

bDbPZO + MoKcIVOGHvLxgTKA =

= bwDm

----- END PGP SIGNATURE -----

You are going to explicitly malloc() space for your array1 entries, and
populate the myArray pointers in array2 with the malloc()ed values.

You are going to do this in program logic.

You are *not* going to do this with an initializer.

HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEzltYagVFX4UWr64RAh+EAJ0XihKh3M+dhmO3sAad8t smGutOFACgq/UR
bDbPZO+MoKcIVOGHvLxgTKA=
=bwDm
-----END PGP SIGNATURE-----



array1 * myArray;
array1 *myArray;



更改为:


array1 myArray;


-


Frederick Gotham


Change to:

array1 myArray;

--

Frederick Gotham




Lew Pitcher写道:

Lew Pitcher wrote:

您将为您的array1条目显式地显示malloc()空间,并使用malloc()ed值填充array2中的myArray指针。


您将在程序逻辑中执行此操作。


您不会*使用初始化程序执行此操作。


HTH

- -

Lew Pitcher


-----开始PGP SIGNATURE -----

版本:GnuPG v1.4.3(MingW32) - WinPT 0.11.12

iD8DBQFEzltYagVFX4UWr64RAh + EAJ0XihKh3M + dhmO3sAad8t smGutOFACgq / UR

bDbPZO + MoKcIVOGHvLxgTKA =

= bwDm

----- END PGP SIGNATURE -----
You are going to explicitly malloc() space for your array1 entries, and
populate the myArray pointers in array2 with the malloc()ed values.

You are going to do this in program logic.

You are *not* going to do this with an initializer.

HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEzltYagVFX4UWr64RAh+EAJ0XihKh3M+dhmO3sAad8t smGutOFACgq/UR
bDbPZO+MoKcIVOGHvLxgTKA=
=bwDm
-----END PGP SIGNATURE-----



其实,我想在array2中使用array1而不指定

array1的大小。 array1的大小是通过初始化列表确定的,

我能做到吗?

struct array2

{

int id;

char * name;

array1 myArray []; - 不会在这里编译

};


actually, I want an array1 inside array2 without specifying the size of
array1. The size of array1 is determined through initialization list,
can I do that?
struct array2
{
int id;
char *name;
array1 myArray[]; - will not compile here
};


这篇关于初始化包含另一个数组的元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆