使用struct的实例作为该struct的成员 [英] Using an instance of a struct as a member of that struct

查看:83
本文介绍了使用struct的实例作为该struct的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我只是想知道这是否可行。我正在尝试在C中实现一个

维特比解码器并创建一个节点数组(结构),

和一个指向节点的指针数组(成员)我很担心)

这样连接它:


//片段开始

typedef struct _node {

char state [2]; //每个节点的状态(00,01,10,11)

int status; // 1:节点在潜在路径上(存在),0:不是
节点* connectedNodes [2]; //连接到节点的ptrs数组

此节点

int edgeCost [2]; //来自节点的边缘成本

int cost; //与节点相关的成本:

}节点;

节点n [4] [maxDepth];

//片段结尾


此行是node * connectedNodes [2];合法吗?


提前感谢您的帮助,

干杯,Tony

Hi all,

I was just wondering if this is possible. I''m trying to implement a
viterbi decoder in C and am creating an array of nodes (the struct),
and an array of pointers to nodes (the member I''m worried about)
connecting to it like so:

// snippet start
typedef struct _node{
char state[2]; // The state of each node ("00","01","10","11")
int status ; // 1: Node is on potential path (exists), 0: not
node* connectedNodes[2]; // Array of ptrs to nodes connected to
this node
int edgeCost[2]; // Cost of edges coming from nodes
int cost; // Cost associated with node:
} node;
node n[4][maxDepth];
//snippet end

Is this line "node* connectedNodes[2];" legal?

Thanks in advance for any help,
Cheers, Tony

推荐答案

>我只是想知道这是否可行。


不,你不能拥有一个结构实例作为其成员

struct。假设结构至少有一个其他成员,

需要无限的内存。


但是,您可以在结构中使用*指针*作为结构的

的成员。链接列表很常见。
>I was just wondering if this is possible.

No, you cannot have an instance of a struct as a member of that
struct. Assuming that the struct has at least one other member,
that would require infinite memory.

You can, however, have a *pointer* to the struct as a member of
that struct. This is common with linked lists.
我正在尝试用C实现一个
维特比解码器并创建一个节点数组(结构),以及指向节点的指针数组(我担心的成员)
如此连接到它:

//片段开始
typedef struct _node {
int status; // 1:节点在潜在路径上(存在),0:不是节点* connectedNodes [2]; //连接到
的节点的ptrs数组

上面应该是struct _node *,而不是节点*。

你还没有定义typedef''node''还没有。

这个节点
int edgeCost [2]; //来自节点的边缘成本
int cost; //与节点相关的成本:
}节点;
节点n [4] [maxDepth];
//代码片段结尾

这行是节点* connectedNodes [2];"合法?
I''m trying to implement a
viterbi decoder in C and am creating an array of nodes (the struct),
and an array of pointers to nodes (the member I''m worried about)
connecting to it like so:

// snippet start
typedef struct _node{
char state[2]; // The state of each node ("00","01","10","11")
int status ; // 1: Node is on potential path (exists), 0: not
node* connectedNodes[2]; // Array of ptrs to nodes connected to
The above should be a struct _node *, not a node *.
You haven''t defined the typedef ''node'' yet.
this node
int edgeCost[2]; // Cost of edges coming from nodes
int cost; // Cost associated with node:
} node;
node n[4][maxDepth];
//snippet end

Is this line "node* connectedNodes[2];" legal?




编号不在上下文中。


Gordon L. Burditt



No. Not in context.

Gordon L. Burditt




" dutchgoldtony" <杜*********** @ gmail.com>在消息中写道

news:11 ********************* @ o13g2000cwo.googlegro ups.com ...

"dutchgoldtony" <du***********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
大家好,


Re:使用结构实例作为结构的成员

我只是想知道这是否可行。


当然不是,经过深思熟虑,你应该明白为什么。

但那不是你要做的事情在下面的代码中。

我正在尝试用C实现一个
维特比解码器并创建一个节点数组(结构),
和一组指针到节点(我担心的成员)
如此连接到它:

//片段开始
typedef struct _node {
char state [2 ]。 //每个节点的状态(00,01,10,11)
int status; // 1:节点在潜在路径上(存在),0:不是节点* connectedNodes [2]; //连接到
此节点的节点的ptrs数组
int edgeCost [2]; //来自节点的边缘成本
int cost; //与节点相关的成本:
}节点;
节点n [4] [maxDepth];
//代码片段结尾

这行是节点* connectedNodes [2];"合法?
Hi all,

Re: Using an instance of a struct as a member of the struct
I was just wondering if this is possible.
Of course not, and with a bit of thought, you should realize why.
But that''s not what you''re trying to do in your code below.
I''m trying to implement a
viterbi decoder in C and am creating an array of nodes (the struct),
and an array of pointers to nodes (the member I''m worried about)
connecting to it like so:

// snippet start
typedef struct _node{
char state[2]; // The state of each node ("00","01","10","11")
int status ; // 1: Node is on potential path (exists), 0: not
node* connectedNodes[2]; // Array of ptrs to nodes connected to
this node
int edgeCost[2]; // Cost of edges coming from nodes
int cost; // Cost associated with node:
} node;
node n[4][maxDepth];
//snippet end

Is this line "node* connectedNodes[2];" legal?




您的编译器说了什么?无论如何,是的,这是合法的。

成员''connectedNodes''不是

类型''struct _node''对象的数组,而是一个数组类型

''struct _node *''(指向''struct _node'')对象的指针。


提醒:

当您使用指针填充该数组时,请确保

它包含有效对象的地址(即您必须首先在某处分配或定义这些对象,
$ $ $
,如果你分配它们,不要忘记释放它们

当你完成它们时。)


-Mike



What did your compiler say? Anyway, yes it''s legal.
The member ''connectedNodes'' is not an array of
type ''struct _node'' objects, but an array of type
''struct _node *'' (pointer to ''struct _node'') objects.

Reminder:
When you populate that array with pointers, be sure
it contains addresses of valid objects (i.e. you must
either allocate or define those objects somewhere first,
and if you allocate them, don''t forget to free them
when you''re done with them).

-Mike


2005-11-14,Gordon Burditt< go *********** @ burditt.org>写道:
On 2005-11-14, Gordon Burditt <go***********@burditt.org> wrote:
我只是想知道这是否可能。
不,你不能拥有一个结构实例作为其成员
结构。假设该结构至少有一个其他成员,那将需要无限的内存。

然而,您可以作为
成员的结构*指针*那个结构。链接列表很常见。
I was just wondering if this is possible.
No, you cannot have an instance of a struct as a member of that
struct. Assuming that the struct has at least one other member,
that would require infinite memory.

You can, however, have a *pointer* to the struct as a member of
that struct. This is common with linked lists.
我正在尝试用C实现一个
维特比解码器并创建一个节点数组(结构),以及指向节点的指针数组(我担心的成员)
如此连接到它:

//片段开始
typedef struct _node {
int status; // 1:节点在潜在路径上(存在),0:不是节点* connectedNodes [2]; //连接到
I''m trying to implement a
viterbi decoder in C and am creating an array of nodes (the struct),
and an array of pointers to nodes (the member I''m worried about)
connecting to it like so:

// snippet start
typedef struct _node{
char state[2]; // The state of each node ("00","01","10","11")
int status ; // 1: Node is on potential path (exists), 0: not
node* connectedNodes[2]; // Array of ptrs to nodes connected to

的节点的ptrs数组



上面应该是struct _node *,而不是节点*。
你还没有定义typedef''node''。


不,它不应该是struct _node *

它应该是struct node *。


typedef struct node {

...

struct node * connectedNodes [2];

...

}节点;

编号不在上下文中。

Gordon L. Burditt



The above should be a struct _node *, not a node *.
You haven''t defined the typedef ''node'' yet.
no, it shouldn''t be struct _node *
it should be struct node *.

typedef struct node {
...
struct node *connectedNodes[2];
...
} node;
No. Not in context.

Gordon L. Burditt



这篇关于使用struct的实例作为该struct的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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