简体   繁体   English

如果 ptr 是指针,那么 typedef ptr p 和 typedef struct ptr p 有什么区别?

[英]If ptr is a pointer,what's the difference between typedef ptr p and typedef struct ptr p?

update: thanx for your help!It's my mistake.更新:感谢您的帮助!这是我的错误。 I see the code from"Data structures and Algorithm Analysis:in c" but I misunderstood it, I tried and the code reported errors,so maybe I am not using it correctly.我看到了来自“数据结构和算法分析:在 c”中的代码,但我误解了它,我尝试过并且代码报告了错误,所以也许我没有正确使用它。 Maybe the code is just sth like Exmaple.也许代码就像 Exmaple 一样。 I found the code in later pages and it is what I thought to be.The problem is solved.我在后面的页面中找到了代码,它是我认为的。问题解决了。

-----------original question: what's the difference between: -----------原始问题:有什么区别:

typedef struct node *ptr;
typedef struct ptr tree;

and:和:

typedef ptr tree?

we can use the second one to create a list, can we define a B-tree in this way?我们可以使用第二个来创建一个列表,我们可以这样定义一个B树吗?

if not, what's the difference?如果不是,有什么区别? (thanks) (谢谢)

First of all you don't really have to write the word struct if node is already defined, so the following is fine:首先,如果已经定义了node则您实际上不必编写单词struct ,因此以下内容很好:

struct node
{
};

typedef node *nptr; //same result as typedef struct node *nptr;

nptr nodePointer = new node();

However, you can use the typedef struct syntax as follows:但是,您可以使用typedef struct语法,如下所示:

typedef struct {int first; int second;} S, *sPtr;
sPtr pointerToS; //same as S * pointerToS

Now, in your example you have现在,在你的例子中,你有

typedef struct node *ptr;
typedef struct ptr tree;

The first line means that ptr now means a pointer to node .第一行表示ptr现在表示a pointer to node The second line basically says "please call the ptr structure a tree from now on".第二行基本上是说“请从现在开始将ptr结构称为”。 However, ptr is a pointer to node , not a struct , that's why you will get an error similar to using typedef-name 'ptr' after 'struct' .但是, ptr是指向node的指针,而不是struct ,这就是为什么您会收到类似于using typedef-name 'ptr' after 'struct'的错误的原因。

The line线

typedef ptr tree;

on the other hand, says "please call ptr a tree from now on", no matter what ptr is, so it compiles fine.另一方面,无论ptr是什么,都说“请从现在开始将ptr称为一棵树”,因此它编译得很好。

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

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