[英]creating Typedef pointers to Typedef structs in C
Have a question about typedef
in C. 在C中有关于
typedef
的问题
I have defined struct: 我已经定义了struct:
typedef struct Node {
int data;
struct Node *nextptr;
} nodes;
How would I create typedef
pointers to struct
Node ?? 我如何创建
struct
node的typedef
指针?
Thanks ! 谢谢 !
You can typedef them at the same time: 你可以同时输入dede:
typedef struct Node {
int data;
struct Node *nextptr;
} node, *node_ptr;
This is arguably hard to understand, but it has a lot to do with why C's declaration syntax works the way it does (ie why int* foo, bar;
declares bar
to be an int rather than an int*
这可能很难理解,但它与C的声明语法为何如此工作有很大关系(即为什么
int* foo, bar;
声明bar
为int而不是int*
Or you can build on your existing typedef: 或者您可以在现有的typedef上构建:
typedef struct Node {
int data;
struct Node *nextptr;
} node;
typedef node* node_ptr;
Or you can do it from scratch, the same way that you'd typedef anything else: 或者你可以从头开始,就像你输入其他任何东西一样:
typedef struct Node* node_ptr;
To my taste, the easiest and clearest way is to do forward declarations of the struct
and typedef
to the struct
and the pointer: 根据我的喜好,最简单,最清晰的方法是对
struct
和指针进行struct
和typedef
前向声明:
typedef struct node node;
typedef node * node_ptr;
struct node {
int data;
node_ptr nextptr;
};
Though I'd say that I don't like pointer typedef
too much. 虽然我会说我不太喜欢指针
typedef
。
Using the same name as typedef
and struct
tag in the forward declaration make things clearer and eases the API compability with C++. 在前向声明中使用与
typedef
和struct
标记相同的名称可以使事情变得更加清晰,并使用C ++简化API兼容性。
Also you should be clearer with the names of your types, of whether or not they represent one node or a set of nodes. 此外,您应该更清楚地了解类型的名称,它们是否代表一个节点或一组节点。
Like so: 像这样:
typedef nodes * your_type;
Or: 要么:
typedef struct Node * your_type;
But I would prefer the first since you already defined a type for struct Node
. 但我更喜欢第一个,因为你已经为
struct Node
定义了一个类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.