首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请问这个比较复杂的结构(包含联合)怎样初始化

2012-03-08 
请教这个比较复杂的结构(包含联合)怎样初始化?#includestddef.h typedefstructArcBox{inttail,head/*该

请教这个比较复杂的结构(包含联合)怎样初始化?
#include   "stddef.h "

typedef   struct   ArcBox
{
int   tail   ,   head;/*   该弧的尾和头顶点的位置   */
struct   ArcBox   *hlink   ,   *tlink;/*   分别为弧头相同和弧尾相同的弧的链域   */
}ArcBox;/*   弧结点   */

typedef   struct   FeaNode
{
char   feaname[30];/*每个特性的名称*/
int   type;/*每个特性的取值类型,0为字符型,1为整型,2为浮点型*/
union   range/*每个特性的取值范围*/
{
char   *c_range[70];
int   i_range[70];
double     d_range[70];
}range;
int   indegree;/*每个特性的入度*/
int   rangenum;/*每个特性取值的数量*/
int   visit[70];/*标记每个父结点的取值*/
struct   ArcBox   *parent,   *child;       /*   分别指向该顶点第一条入弧和出弧   */
}FeaNode;/*   顶点结点   */

typedef   struct
{
struct   FeaNode   *fealist;/*   指向表头向量(数组)   */
int   feanum   ,   arcnum;/*   贝叶斯网络的当前顶点数和弧数   */
}BayesNetwork;

void   InitNB(BayesNetwork   *nb)
{
struct   FeaNode   fealist[5]=
{{ "duration ",1,{0,1},0,2,{0},NULL,NULL},   /*34行*/
  { "protocol_type ",0,{ "icmp ", "tcp ", "udp "},0,3,{0},NULL,NULL},
  { "srv_diff_host_rate ",2,{0,0.3,0.6,0.9,1},0,5,{0},NULL,NULL},/*36行*/
  { "src_bytes ",1,{0,150,450,5000},0,4,{0},NULL,NULL},/*37行*/
  { "dst_host_rerror_rate ",2,{0.0,1.0},0,2,{0},NULL,NULL}};/*38行*/
(*nb).fealist=fealist;
(*nb).feanum=5;
(*nb).arcnum=0;
}

编译后,出现的错误提示:
Compiling...
Test.C
D:\My   Documents\Test\Test.C(34)   :   warning   C4047:   'initializing '   :   'char   * '   differs   in   levels   of   indirection   from   'const   int   '
D:\My   Documents\Test\Test.C(36)   :   error   C2115:   'initializing '   :   incompatible   types
D:\My   Documents\Test\Test.C(36)   :   error   C2115:   'initializing '   :   incompatible   types
D:\My   Documents\Test\Test.C(36)   :   error   C2115:   'initializing '   :   incompatible   types
D:\My   Documents\Test\Test.C(36)   :   warning   C4047:   'initializing '   :   'char   * '   differs   in   levels   of   indirection   from   'const   int   '
D:\My   Documents\Test\Test.C(37)   :   warning   C4047:   'initializing '   :   'char   * '   differs   in   levels   of   indirection   from   'const   int   '
D:\My   Documents\Test\Test.C(37)   :   warning   C4047:   'initializing '   :   'char   * '   differs   in   levels   of   indirection   from   'const   int   '
D:\My   Documents\Test\Test.C(37)   :   warning   C4047:   'initializing '   :   'char   * '   differs   in   levels   of   indirection   from   'const   int   '
D:\My   Documents\Test\Test.C(38)   :   error   C2115:   'initializing '   :   incompatible   types
D:\My   Documents\Test\Test.C(38)   :   error   C2115:   'initializing '   :   incompatible   types


Error   executing   cl.exe.

书上说不能在联合定义的时候初始化,那个对于我这个结构(包括联合)该怎样初始化呢?如果用scanf太烦琐了。如果用循环,我的这些数据似乎也没有什么规律(我贴出来的是简化了的,实际的数据量很大)。如果我贴出来的初始化方法可行,那么该怎样修改呢?多谢了!

[解决办法]
struct FeaNode fealist[5]=
{{ "duration ",1,{0},0,2,{0},NULL,NULL}, /*34行*/
{ "protocol_type ",0,{ "icmp ", "tcp ", "udp "},0,3,{0},NULL,NULL},
{ "srv_diff_host_rate ",2,{0},0,5,{0},NULL,NULL},/*36行*/
{ "src_bytes ",1,{0},0,4,{0},NULL,NULL},/*37行*/
{ "dst_host_rerror_rate ",2,{0},0,2,{0},NULL,NULL}};/*38行*/

fealist[0].range.i_range[1]=1;
fealist[2].range.d_range[1]=0.3;
fealist[2].range.d_range[2]=0.6;
fealist[2].range.d_range[3]=0.9;
... //然后给各个联合体元素赋值


如果不是字符串的,
联合体不接受初始化, //只能初始化 union 中的第一个成员类型的数据
只能初始化为 0,
然后根据需要赋值了 。

热点排行