简体   繁体   English

是否可以制作一个指向struct(节点)的指针数组?

[英]Is it possible to make an array of pointers to struct (node)?

So I'm trying to make a prefix tree, but I'm not sure if this would cause some logical error of some sort? 所以我正在尝试制作一个前缀树,但是我不确定这是否会导致某种逻辑错误?

typedef struct TreeTag
{
    char letter;
    struct TreeTag *links[26]; /* Is this advisable? */
    int fullword;
    int linknum;
}TreeNode;

Yes, array of pointers to struct node is allowed 是的,允许指向结构节点的指针数组

If you do this: 如果您这样做:

struct node * p; //it's allowed

so there is no problem to declare array of pointers. 因此声明指针数组没有问题。

When you need the more than one pointers to the same struct you do this 当您需要多个指向同一结构的指针时,请执行此操作

struct node * left;
 strcut node * right;

so why can't you do this 那你为什么不能这样做

struct node *child[2];

Similarly struct node *p[26] is also possible but everything depends on your requirement and implementation. 同样, struct node *p[26]也可以,但是一切都取决于您的要求和实现。

One requirement , As I think think of d-ary tree (where each node has d nodes) And you want to directly nevigate to it's children from parent node. 一个需求,就像我想到的d-ary tree (每个节点都有d个节点)一样,您想直接从父节点导航到它的子节点。

so struct node *child[d] is legal (where d is #defined ) 因此struct node *child[d]是合法的(其中d是#defined

That's fine. 没关系。 You can use pointers to the struct inside the struct. 您可以在结构内部使用指向该结构的指针。

The struct is considered declared, but not defined, at the point you declare the member, and pointers to declared, but not yet defined types, are common. 在声明成员时,该结构被视为已声明但尚未定义,并且指向已声明但尚未定义的类型的指针很常见。 How would you, for example, be able to define the nodes in a binary tree or a linked list otherwise? 例如,您将如何在二叉树或链表中定义节点? For a list, you would normally do something like 对于列表,您通常会做类似

typedef struct ListItem {
   struct ListItem * next;
   SomeType data;
} ListItem;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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