指针在C自我结构 [英] Pointer to self struct in C

查看:182
本文介绍了指针在C自我结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code:

typedef struct elementT {
  int data;
  struct elementT *next;
} element;

为什么有必要做结构elementT *下一,我不能做元素*下一里面的结构声明本身?是不是因为它是尚未宣布?

Why is it necessary to do struct elementT *next and I can't do element *next inside the struct declaration itself? Is it because it is not declared yet?

推荐答案

的typedef 只发生后,结构已被定义。请看下面的code;语法是无效的,但它希望它显示订单/ $ P $的东西pcedence:

The typedef only takes place after the struct has been defined. Consider the following code; the syntax is invalid, but it hopes it shows the order/precedence of things:

typedef (struct {
  int field1, field2;
}) item;

即,在结构{...} 是一个前pression,它表示一个类型。通过给它一个名称的该类型的typedef 工作

I.e., the struct{...} is an "expression" that denotes a type. The typedef operates on that type by giving it a name.

相反,

struct foo {
    foo *next;
};

工程的C因为两个特殊的规则:结构<名称> 不仅定义了一个类型,但给它一个结构标记为好,而标签是结构声明的其余部分(原因很明显内立即可见:链表,树等,将是非常痛苦没有这些规则来实现)。

works because of two special rules of C: struct <name> not only defines a type, but gives it a struct tag as well, and that tag is immediately visible inside the rest of the struct declaration (for the obvious reason: linked lists, trees, etc. would be very painful to implement without these rules).

这篇关于指针在C自我结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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