struct typedef中的声明规则

时间:2012-11-09 06:26:24

标签: c struct typedef declaration

我正在阅读“C编程语言”并遇到了关于 struct 的typedef的问题。代码是这样的:

typedef struct tnode *Treeptr;
typedef struct tnode { /* the tree node: */
    char *word; /* points to the text */
    int count; /* number of occurrences */
    struct tnode *left; /* left child */
    struct tnode *right; /* right child */
} Treenode;

到我们写的时候

typedef struct tnode *Treeptr;

tnode仍未声明,但我们没有收到任何编译错误, 但是当我们将上述陈述改为:

typedef Treenode *Treeptr;

我们收到编译错误:

error: parse error before '*' token
warning: data definition has no type or storage class

造成差异的原因是什么? “struct tnode”不是和“Treenode”一样吗?

3 个答案:

答案 0 :(得分:6)

在定义之前不能使用类型。

使用typedef struct tnode { ... } Treenode;声明,直到达到分号才定义类型Treenode

typedef struct tnode *Treeptr;的情况不同。这告诉编译器'有一个名为struct tnode的结构类型,类型Treeptr是指向struct tnode'的指针的别名。在该声明的最后,struct tnode是一个不完整的类型。您可以创建指向不完整类型的指针,但不能创建不完整类型的变量(因此您可以定义Treeptr ptr1;struct tnode *ptr2;,它们是相同的类型,但您无法定义struct tnode node;

struct tnode的正文可以写成:

typedef struct tnode
{
    char    *word;
    int      count;
    Treeptr  left;
    Treeptr  right;
} Treenode;

因为Treeptr是定义结构之前类型struct tnode *的已知别名。您不能使用Treenode *left;因为Treenode在达到最终分号之前不是已知的别名(粗略地说)。

答案 1 :(得分:1)

当您声明TreePtr时,您没有实现该结构。这被称为“前向声明”。类似的事情:“我们在这里使用它,但后来我会更好地解释它”。该实现必须稍后出现,只能出现一次,这就是您在第二个typedef中找到的内容。

并且TreePtr与结构不同,因为TreePtr实际上是一个新类型,包括指针的事实。

答案 2 :(得分:0)

typedef struct tnode *Treeptr;具有“tnode”结构的隐式前向声明。它类似于:

typedef struct tnode Treenode;
typedef Treenode *Treeptr;

struct tnode { /* the tree node: */
    char *word; /* points to the text */
    int count; /* number of occurrences */
    struct tnode *left; /* left child */
    struct tnode *right; /* right child */
};