问题描述
我是C语言的新手,所有指针内容都有一些问题.
I'm pretty new in C and having some problems with all the pointer stuff.
我写了这段代码:
typedef struct edgeitem
{
double weight;
}EDGE_ITEM, *pEDGE_ITEM; //also declaration of a pointer
/************ edge that includes a pointer to the edge item, next and prev ******/
typedef struct edge
{
pEDGE_ITEM *edge_item;
pEDGE *next; //pointer to next edge
pEDGE *prev; //pointer to prev edge
}EDGE, *pEDGE;
这样我会出错,只是不明白为什么.
I get an error this way and just cant understand why.
我知道edgeitem
和edge
是标签,可以使用struct edge *next
,但是我声明了指针,所以我怎么不能使用它们呢?
I know that edgeitem
and edge
are tags and I can use struct edge *next
but I declared the pointers so how come i can't use them?
如果我有指针,我需要使用*吗?
Do i need to use * if I have a pointer?
pEDGE_ITEM *edge_item
//or
pEDGE_ITEM edge_item
我不明白,它是一个指针,所以为什么要添加*?
I cant understand, it's a pointer so why do I add the *?
最后一个问题是:如果我使用上面的方法,两者之间有什么区别?
And the last question is:If I'm using the above, what's the difference between:
*EDGE next
EDGE *next
最后一个:如果我要添加:
and last one :if I'm adding:
typedef struct edge_list
{
EDGE *head;
}EDGE_LIST;
与:
pEDGE head;
推荐答案
您不能在结构定义中使用pEDGE.您应该执行以下操作:
You cannot use pEDGE within the definition of the struct. You shoud do something like:
typedef struct edge {
pEDGE_ITEM *edge_item;
struct edge *next; //pointer to next edge
struct edge *prev; //pointer to prev edge
} EDGE, *pEDGE;
您还必须注意,edge_item
是双指针.您在问题中也提到了这一点.因此,如果您使用pEDGE_ITEM
并且只想拥有一个普通的指针,则不应该编写pEDGE_ITEM *edge_item
,而只需编写pEDGE_ITEM edge_item
.
You must also note that edge_item
is a double pointer. You also mention that in your question. So if you use pEDGE_ITEM
and you just want to have a normal pointer you should not write pEDGE_ITEM *edge_item
but just pEDGE_ITEM edge_item
.
为澄清起见,以下所有声明都是等效的:
For clarifying, all the following declarations are equivalent:
struct edgeitem *edge_item;
EDGE_ITEM *edge_item;
pEDGE_ITEM edge_item;
但是
pEDGE_ITEM *edge_item;
等同于
struct edgeitem **edge_item;
EDGE_ITEM **edge_item;
关于*EDGE next
的语法错误.正确的语法为EDGE* next
或pEDGE next
.因此,一旦定义了struct edge
,您就可以使用这两个中的任何一个,但是在定义结构时,您必须按照我在答案开头所显示的那样进行操作.
About *EDGE next
that is wrong syntax. The correct syntax would be EDGE* next
or pEDGE next
. So once struct edge
is defined you can just use any of these two, but while defining the struct, you must do as I show at the beginning of my answer.
是的,以下两个定义是等效的:
Yes, the following two definitions are equivalent:
typedef struct edge_list {
EDGE *head;
} EDGE_LIST;
typedef struct edge_list {
pEDGE head;
} EDGE_LIST;
这篇关于typedef结构指针定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!